本文概述:
可能会存在一些需求,一些部署在内部的或者单独一个环境的服务,提供rest服务。由后端来发送http请求来调用这些接口。那在后端就需要构造http请求。本文要解决的场景如下:
前端multipartfile 上传文件,后端接收后构造post请求,调用外部接口,传送文件流及其他参数
内容包括:
1.前端postman调用上传接口
2.后端接收multipartfile文件
3.后端用httpclient构造post请求
4.调用外部接口后返回结果的处理
先上代码:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
/**
*前端上传文件,后端接收后构造post请求,调用外部文件上传接口
*/
@PostMap