使用HttpClients发送从HttpServletRequest获取的文件以及接受
一:接收发送
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.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;
import org.apache.http.util.EntityUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.Charset;
import java.util.Map;
public void doPost(HttpServletRequest request, HttpServletResponse response) {
GgRiskConfigValueDto poaserialno_switch2 = ServiceManager.prpall.getGgRiskConfigValueService().findByPrimaryKey("0303", "0000", "TPIS-00053");
String url = poaserialno_switch2.getConfigValue();
try {
MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
MultipartHttpServletRequest multipartHttpServletRequest = resolver.resolveMultipart(request);
Map<String, MultipartFile> fileMap = multipartHttpServletRequest.getFileMap();
MultipartFile file = null;
CloseableHttpClient httpClient = HttpClients.createDefault();
String result = "";
HttpEntity httpEntity = null;
HttpEntity responseEntity = null;
HttpPost httpPost = new HttpPost(url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setCharset(Charset.forName("utf-8"));
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
for (Map.Entry<String,MultipartFile> entry : fileMap.entrySet()){
file =entry.getValue();
if (!file.isEmpty()){
builder.addBinaryBody("file", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, file.getName());
httpEntity = builder.build();
httpPost.setEntity(httpEntity);
HttpResponse response1 = httpClient.execute(httpPost);
responseEntity = response1.getEntity();
if (responseEntity != null) {
result = EntityUtils.toString(responseEntity,Charset.forName("UTF-8"));
JSONObject jsonObject = JSONObject.parseObject(result);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
二:接收端
public Result<List<实体类>> uploadConfigFilterExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
EasyExceLlistener.getFaillist().clear();
Long l = System.currentTimeMillis();
ServletContext context = request.getSession().getServletContext();
CommonsMultipartResolver resolver = new CommonsMultipartResolver(context);
if (resolver.isMultipart(request)) {
MultipartHttpServletRequest multRequest = (MultipartHttpServletRequest) request;
MultipartFile file = multRequest.getFile("file");
获取文件后可以对文件进行其他操作
}
System.out.println("所有数据存储完毕总耗时:" + (System.currentTimeMillis() - l) / 1000 + "秒");
return ResultUtil.success(EasyExceLlistener.getFaillist());
}