遇到中文名的图片是无法上传到tomcat上面的,所以设置utf-8加UUID才可以解决
@ResponseBody
@PostMapping("/add")
public Object add(Inventory inventory,FileAll fileall, HttpServletRequest request) throws IOException {
// MultipartFile file = fileAll.getFileKe();
MultipartFile file = fileall.getFile();
System.out.println(file);
System.out.println(inventory);
if (!file.isEmpty()) {
// 上传文件路径
// String UploadPath = uploadPath
// System.out.println(uploadPath);
//取得原文件名字
String fileName = file.getOriginalFilename();
String fileAlls = tomcat+fileName;
System.out.println(fileAlls);
//为了保证图片在服务器中名字的唯一性,用UUID对filename进行改写
String encode = URLEncoder.encode(fileName, "UTF-8");
String uuid = UUID.randomUUID().toString().replace("-","");
//将生成的uuid和filename进行拼接
String newFilename = uuid +"-"+encode;
// 文件保存到另一个服务器上去了
// 创建 sun公司提供的jersey包中的client对象
Client client=Client.create();
WebResource resource = client.resource(tomcat+newFilename);
// 文件保存到另一个服务器上去了
resource.put(String.class, file.getBytes());
inventory.setImgPath(newFilename);
inventoryService.save(inventory);
}
return "success";
}