@RequestMapping(value = "/uploadChatPicture", method = { RequestMethod.GET, RequestMethod.POST })
@ResponseBody
public Map<String, Object> upload(HttpServletResponse resp, @RequestParam("file") MultipartFile file) {
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
Map<String, Object> map = new HashMap<>();
File f = null;
try {
httpClient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(200000).setSocketTimeout(200000000).build();
//把一个普通参数和文件上传给下面这个地址 是一个servlet
HttpPost httpPost = new HttpPost(myConfig.getImage_save());
httpPost.setConfig(requestConfig);
InputStream ins = file.getInputStream();
f = new File(file.getOriginalFilename());
inputStreamToFile(ins, f);
MultipartEntityBuilder multipartEntity = createMultipart(f, file.getOriginalFilename());
HttpEntity reqEntity = multipartEntity.build();
httpPost.setEntity(reqEntity);
// 发起请求 并返回请求的响应
response = httpClient.execute(httpPost);
System.out.println("The response value of token:" + response.getFirstHeader("token"));
// 获取响应对象
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
resEntity.getContent().toString();
String responseEntityStr = EntityUtils.toString(response.getEntity());
JSONObject jsonobj = JSONObject.fromObject(responseEntityStr.substring(2, responseEntityStr.length() - 1));// 将字符串转化成json对象
if ("1".equals(jsonobj.get("code").toString())) {
map.put("code", 0);
Map<String, Object> data = new HashMap<>();
data.put("src", myConfig.getImage_URL() + jsonobj.getString("filename"));
map.put("data", data);
} else {
map.put("code", 1);
}
}
// 销毁
EntityUtils.consume(resEntity);
} catch (Exception e) {
map.put("code", 0);
map.put("msg", "上传图片失败!");
e.printStackTrace();
} finally {
}
return map;
}
//创建httpclient参数(包含file,需要指定类型,不然被调用的方法不能识别类型,会指定为无类型文件)
private MultipartEntityBuilder createMultipart(File f, String originalFilename) {
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
if (originalFilename.endsWith(".bmp")) {
multipartEntityBuilder.addBinaryBody("file", f, ContentType.IMAGE_BMP, originalFilename);
} else if (originalFilename.endsWith(".gif")) {
multipartEntityBuilder.addBinaryBody("file", f, ContentType.IMAGE_GIF, originalFilename);
} else if (originalFilename.endsWith(".jpeg")) {
multipartEntityBuilder.addBinaryBody("file", f, ContentType.IMAGE_JPEG, originalFilename);
} else if (originalFilename.endsWith(".png")) {
multipartEntityBuilder.addBinaryBody("file", f, ContentType.IMAGE_PNG, originalFilename);
} else if (originalFilename.endsWith(".svg")) {
multipartEntityBuilder.addBinaryBody("file", f, ContentType.IMAGE_SVG, originalFilename);
} else if (originalFilename.endsWith(".tiff")) {
multipartEntityBuilder.addBinaryBody("file", f, ContentType.IMAGE_TIFF, originalFilename);
} else if (originalFilename.endsWith(".webp")) {
multipartEntityBuilder.addBinaryBody("file", f, ContentType.IMAGE_WEBP, originalFilename);
}else if (originalFilename.endsWith(".jpg")) {
multipartEntityBuilder.addBinaryBody("file", f, ContentType.IMAGE_JPEG, originalFilename);
}
return multipartEntityBuilder;
}
//将文件写入到本地,
public void inputStreamToFile(InputStream ins, File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
}