Java中 HttpClient中转上传文件遇到的坑

  • 遇到的坑:
    •  查询了网上的博客,百分之99的人在添加字节流文件的时候都是这么写:
      MultipartEntityBuilder builder = MultipartEntityBuilder.create();
      builder.addBinaryBody("file", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
      builder.addTextBody("filename", fileName);// 类似浏览器表单提交,对应input的name和value
      HttpEntity entity = builder.build();
      httpPost.setEntity(entity);

       

    •   我在发送的时候总是报错,最后看了下addTextBody的其他方法,我们可以直接把字节数组传进去,试了一下成功了
      MultipartEntityBuilder builder = MultipartEntityBuilder.create();
      builder.setCharset(StandardCharsets.UTF_8);
      // 文件流 
      builder.addBinaryBody("filename",file.getBytes());
      HttpEntity entity = builder.build();
      httpPost.setEntity(entity);

       

  • 使用具体步骤:
    • 1.需要导入的pox.xml:
      <dependency>
              <groupId>org.apache.httpcomponents</groupId>
              <artifactId>httpclient</artifactId>
              <version>4.4</version>
      </dependency>
      <dependency>
              <groupId>org.apache.httpcomponents</groupId>
              <artifactId>httpmime</artifactId>
              <version>4.4</version>
      </dependency>

       

    • 具体代码:
      public static String httpClientUploadFile(String url, MultipartFile file, Map<String, String> header) {
              CloseableHttpClient httpClient = HttpClients.createDefault();
              String result = "";
              try {
                  HttpPost httpPost = new HttpPost(url);
                  //请求头
                  if (MapUtils.isNotEmpty(header)) {
                      for (Map.Entry<String, String> entry : header.entrySet()) {
                          httpPost.addHeader(entry.getKey(), entry.getValue());
                      }
                  }
                  MultipartEntityBuilder builder = MultipartEntityBuilder.create();
                  builder.setCharset(StandardCharsets.UTF_8);
                  // 文件流 
                  builder.addBinaryBody("filename",file.getBytes());
                  HttpEntity entity = builder.build();
                  httpPost.setEntity(entity);
                  // 执行提交
                  HttpResponse response = httpClient.execute(httpPost);
                  HttpEntity responseEntity = response.getEntity();
                  if (responseEntity != null) {
                      // 将响应内容转换为字符串
                      result = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
                  }
              } catch (Exception e) {
                  log.error(e.getMessage());
              } finally {
                  try {
                      httpClient.close();
      
                  } catch (IOException e) {
                      log.error(e.getMessage());
                  }
              }
              return result;
          }

       

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值