//这是一个以前的接口,采用.do的链接方式,现在一般都不会用了,最近的工作就是从以前很老的项目中进行改造,所以遇到,
//所以参数都是放在request的body中,这样就需要从request取出file流,然后再调另外服务器接口进行文件上传到另外的一个服务//器中
//这里主要了解了从request请求获取file,和怎么样用httpclient去上传文件
/**
* request请求获取file
*
* 将request请求的上下文转换为MultipartResolver,然后转换为MultipartHttpServletRequest请求,
* 通过multi请求就可以获取对应的file文件信息,这样的方法没有问题,后台能获取到相应的参数;
* 参考:https://www.cnblogs.com/yskcoder/p/4718198.html
* 多看源码
*/
public ModelAndView attachUploadForOnline(HttpServletRequest request, HttpServletResponse response)
throws Exception {
JSONObject mapResult = new JSONObject();
//需要请求的链接
String url = “请求路径”
try {
//从request取出file的方法
MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(request);
MultipartFile file = multipartRequest.getFile("form-file");
//
String indexTable = request.getParameter("indexTable");
String indexId = request.getParameter("indexId");
// 附件服务器地址
String remote_url = url + "/common.do?method=attachUpload&indexTable=" + indexTable + "&indexId=" + indexId;
//这一步就是去请求另外服务的接口,采用post表单模拟文件上传请求
String result = HttpClientUntil.postFile(file, remote_url);
mapResult.put("code", Constants.FAIL_CODE);
mapResult.put("msg", Constants.FAIL_MSG);
} catch (Exception e) {
e.printStackTrace();
mapResult.put("code", Constants.FAIL_CODE);
mapResult.put("msg", Constants.FAIL_MSG);
}
return new ModelAndView(Constants.RESULT_VIEW, mapResult);
}
///这是httpclient请求的工具类,参考网上的还有自己修改
package com.zh.audit.app.until;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
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 com.zh.framework.pub.sys.UTILSysProperty;
public class HttpClientUntil {
public static String postFile(MultipartFile file, String remote_url) {
CloseableHttpClient httpClient = HttpClients.createDefault();
String result = "";
try {
String fileName = file.getOriginalFilename();
HttpPost httpPost = new HttpPost(remote_url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
//这是设置buider编码格式的,不然传过去的文件名会乱码,HttpMultipartMode这个类很关键
//https://www.cnblogs.com/evasean/archive/2018/07/25/9368670.html
//https://blog.csdn.net/u012685794/article/details/51755799,这是两篇解释的博客
builder.setCharset(Charset.forName("utf-8"));
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("form-file", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
builder.addTextBody("filename", fileName);// 类似浏览器表单提交,对应input的name和value
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
HttpResponse res = httpClient.execute(httpPost);// 执行提交
HttpEntity responseEntity = res.getEntity();
if (responseEntity != null) {
// 将响应内容转换为字符串
result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
public static String doGet(String url) {
return doGet(url, null);
}
public static String doGet(String url, Map<String, String> param) {
// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
String resultString = "";
CloseableHttpResponse response = null;
try {
// 创建uri
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
// 创建http GET请求
HttpGet httpGet = new HttpGet(uri);
// 执行请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultString;
}
}