File 参数 模拟POST请求

    public String OperationOrderSend(Map<String,String> params,String apiMethodName)
        throws IOException {
            Map<String,String> textParams=new HashMap<String,String>(params); //文本请求参数 
            Map<String,String> fileParams=new HashMap<String,String>(); //文件请求参数
            if(apiMethodName.equals("sendGoods")){
                fileParams.put("sendGoods",params.get("sendGoods"));
                textParams.remove("sendGoods");
            }
            textParams.put("gShopID", shopId);
            String validate = getValidateString(apiMethodName, textParams, apiKey);
            System.out.println("是这个验证码么:"+validate);
            
            String targetURL = "http://api.dangdang.com/v2/sendGoods.php"; // servleturl
            HttpClient httpClient = new HttpClient();
            PostMethod postMethod = new PostMethod(targetURL);
            
            
            
            String temp = null;
            temp = ServletActionContext.getServletContext().getRealPath("");
            temp = temp+"//temp//temp.xml";
            
            File file = new File(temp);
            Part [] parts = new Part[3];
            parts[0] = new StringPart("gShopID",shopId);
            try {
                parts[1] = new FilePart("sendGoods", file,"text/xml","GBK");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }//第三个参数就是说明这个文件类型是xml的,不用这个的话,接口会返回说上传的文件不是xml类型
            parts[2] = new StringPart("validateString",getValidateString(apiMethodName, textParams, apiKey)); 
            postMethod.setRequestEntity(new    MultipartRequestEntity(parts,postMethod.getParams())); 
            int statusCode = httpClient.executeMethod(postMethod);
            System.out.println("反馈信息为:"+new String(postMethod.getResponseBody()));;
            String info = new String(postMethod.getResponseBody());
            postMethod.releaseConnection();
            return info;
    }
原文地址:https://www.cnblogs.com/jayGold/p/3325915.html