file的name为files
@RequestMapping ("/publish")
@ResponseBody
public WebJsonBean publish(@RequestParam MultipartFile[] files) throws IOException {
try {
for (int i = 0; i < files.length; i++) {
if (files[i].isEmpty()){
throw new MorningStarException(CODE.FAIL);
}
logger.info("upload path====>{}",path);
File fileout = new File(path);
if(!fileout.exists()){
fileout.getParentFile().mkdir();
fileout.mkdir();
}
file.transferTo(fileout);
}
} catch (IOException e) {
logger.info("文件上传异常", e);
}
return null;
}
重点
- 1.必须要判断文件夹是否存在
2.我记得以前是不要的判断的,不存在就默认创建,难道改了?
浪费了三小时,长记性………………
本文介绍了一个文件上传的方法,包括如何检查文件夹是否存在并进行相应的创建操作。通过示例代码展示了如何处理多个上传文件,并强调了确保文件夹存在的必要性。
1238





