controller层
public @ResponseBody
Object uploadEditorImg(@RequestParam("img") MultipartFile files[], HttpServletRequest request) {
String sPath = request.getRealPath("/");
List<String> picUrls = new ArrayList<>();
for (MultipartFile file: files) {
try {
String picUrl = imageService.uploadImg2FileSys(file, sPath,true);
picUrls.add(picUrl);
} catch (Exception e) {
e.printStackTrace();
return buildError("文件上传出错"+e.getMessage());
}
}
return buildSucces(picUrls);
}
public String uploadImg2FileSys(MultipartFile file, String sPath, Boolean isRandomName) {
String fileName;
if (isRandomName) {
fileName = UUID.randomUUID() + file.getOriginalFilename();
} else {
fileName = file.getOriginalFilename();
}
String imgUploadRelativePath = "upload/img/afc/nodeCategory/";
String pathname = sPath + imgUploadRelativePath;
File newfile1 = new File(pathname);
if (!newfile1.exists()) {
newfile1.mkdirs();
}
File imgFile = new File(newfile1, fileName);
try {
file.transferTo(imgFile);
} catch (IOException e) {
e.printStackTrace();
}
return "/" + imgUploadRelativePath + fileName;
}