HttpClient post多文件上传

public void post() throws ClientProtocolException, IOException{
        //创建client
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            //创建post请求
            HttpPost httppost = new HttpPost(url);
            //文件
            FileBody file1 = new FileBody(new File("F:/download/bc09df07e24c253cc6de4605a9eea07b.jpg"));
            FileBody file2 = new FileBody(new File("F:/download/3feededd6b766d14267eef4c429943c1.jpg"));
            //参数 
            //
            StringBody lng = new StringBody("33.33", ContentType.TEXT_PLAIN);  
            StringBody lat = new StringBody("44.33", ContentType.TEXT_PLAIN); 
            //组建访问实体
            //如 spring-mvc @RequestParam("lng")Double lng
            //             @RequestParam("lat")Double lat
            //             @RequestParam("file")MultipartFile[] files
            HttpEntity reqEntity = MultipartEntityBuilder.create()
                    .addPart("file", file1)
                    .addPart("file", file2)
                    .addPart("lng", lng)
                    .addPart("lat", lat)
                    .build();
            //设置实体
            httppost.setEntity(reqEntity);
            //设置头信息
            httppost.setHeader("module", "love");
            httppost.setHeader("code", "111111");
             
            System.out.println("executing request " + httppost.getRequestLine());
            //执行post请求
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                System.out.println("----------------------------------------");
                //状态
                System.out.println(response.getStatusLine());
                //响应实体
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    System.out.println(EntityUtils.toString(resEntity));
                }
                //关闭HttpEntity流
                EntityUtils.consume(resEntity);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }




转载于:https://my.oschina.net/u/2385255/blog/552180

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值