HttpClient - multipart/form-data文件中转上传

使用httpClient 中转上传 multipart/form-data 类型文件

客户端提交上传文件 —> a服务器接收 —> 转发到b服务器进行文件处理或存储

引入依赖:

 <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>

上传代码:

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Service
public class PutFileServiceImpl extends BaseServiceImpl {

    @Override
    public BaseResult fileUpload(MultipartFile file) {
        BaseResult bslBaseResult = null;
        if (file.isEmpty()) {
            throw new BusinessException("文件为空,上传失败");
        }
        String url = "xxxxxxxxxxxxxxx";
        String result = null;
        try {
            HttpPost httpPost = new HttpPost(url);

            //注:这里加上Content-Type为multipart/form-data属性会上传失败,报500。
            //因为这里面有个boundary参数属性作为mulipart类型请求数据的"分割边界",这个值一般是由浏览器随机生成的,
            //如果强行指明很可能导致边界值不一致,就会请求失败。详见 https://blog.csdn.net/xiaojianpitt/article/details/6856536
//            httpPost.setHeader("Content-Type", ContentType.MULTIPART_FORM_DATA.toString());
            String fileName = file.getOriginalFilename();
            ContentBody fileContentBody = new ByteArrayBody(file.getBytes(), fileName);

            MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
            entityBuilder.addPart("file", fileContentBody);
            HttpEntity httpEntity = entityBuilder.build();
            httpPost.setEntity(httpEntity);

			// 执行提交
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpResponse response = httpClient.execute(httpPost);

            HttpEntity entity = response.getEntity();
            if (entity != null) {
                // 将响应内容转换为字符串
                result = EntityUtils.toString(entity, StandardCharsets.UTF_8);
                // 将响应内容字符串解析为对象
                bslBaseResult = JSONObject.parseObject(result, BslBaseResult.class);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bslBaseResult;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值