HttpClient请求传输文件到目标项目. MulitPartFile
- 业务需要,需要程序调用文件服务器然后再进行处理文件,回头更新下接受的控制层代码,因为那是另一位同时写的,首先文件是从前端直接传过来的,就是用MulitPartFile接受的文件,不可以使用在之前使用file.transferTo()方法,这样的话在执行下面的代码的时候就会出现文件已经被迁移.
String fileUrl = fileManager + "/fileAPI/uploadFile";//服务器地址.
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(fileUrl);
MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.setCharset(Charset.forName("UTF-8")).addBinaryBody("files", file.getInputStream(), ContentType.MULTIPART_FORM_DATA, file.getOriginalFilename());
builder.addTextBody("snapshotID",snapshotID);
builder.addTextBody("sign", MDX.md5(snapshotID+Constant.SIGN_KEY+ com.bqdata.ad.core.util.DateUtils.formatDate(new Date())).toUpperCase());
System.out.println(com.bqdata.ad.core.util.DateUtils.formatDate(new Date()));
//builder.addTextBody("areaMemo",clue.getMemo());
//这里处理的中文会乱码
ContentType contentType =ContentType.create(HTTP.PLAIN_TEXT_TYPE,HTTP.UTF_8);
StringBody stringBody = new StringBody(clue.getMemo(),contentType);
builder.addPart("areaMemo",stringBody);
builder.addTextBody("latitudeAndLongitude", clue.getLatitude()+" "+clue.getLongitude());
builder.addTextBody("daqTime",clue.getDaqTime().toString());
builder.addTextBody("type",Constant.SNAPSHOT_OUTDOOR_VIDEO);
HttpEntity httpEntity = builder.build();
httpPost.setEntity(httpEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity responseEntity = httpResponse.getEntity();
result= EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
System.out.println(result);
嗨害嗨,这里全处理一下乱码的方式
public static String updateFilesPost(String url, MultipartFile file, Map<String, String> requestMap) {
url += "/fileAPI/uploadFile";
String sign = "";
String result = "";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
logger.info(url);
try {
MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.setCharset(StandardCharsets.UTF_8)
.addBinaryBody("files",file.getInputStream(),ContentType.MULTIPART_FORM_DATA,file.getOriginalFilename());//中转文件输入流以mulitpart/form-data 格式传输。
//遍历map 给每个可能会乱码的中文进行处理。
for (Map.Entry<String, String> entry : requestMap.entrySet()) {
ContentType contentType = ContentType.create(HTTP.PLAIN_TEXT_TYPE, HTTP.UTF_8);
StringBody stringBody = new StringBody(entry.getValue(), contentType);
builder.addPart(entry.getKey(), stringBody);
}
HttpEntity httpEntity = builder.build();
httpPost.setEntity(httpEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity responseEntity = httpResponse.getEntity();
result = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
logger.info(result);
} catch (IOException e) {
logger.error("调用文件服务异常");
e.printStackTrace();
}
return result;
}
接受文件接口方法如下
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public Response uploadFile(ReceiveFileModel model) {
// 参数校验
MultipartFile[] files = model.getFiles();
if (files != null && files.length == 0) {
return new Response().failure(RespConstant.ERROR_PARAM.getCode());
此后省略业务逻辑。。。。。。
可以看出和我们平常写的接口没有什么区别,这里用了一个接受参数的实体,其中files 就是 MultipartFile 类型的 变量。
private String snapshotID;
private String areaMemo;
private String latitudeAndLongitude;
private String daqTime;
private String sign;
private MultipartFile[] files;