org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.
这是文件上传时的一个bug,其原因是因为文件上传的大小过大,超出了默认的文件上传的大小,我是使用的Thymeleaf模板引擎进行的文件上传所以是在application.yml文件中进行修改加上文件传输的大小即可
spring:
servlet:
multipart:
max-file-size: 10MB
将这些配置好后就可以进行文件传输了
后台端口形如:(可以多文件上传)
代码为:
/**
* 图片上传
* @param file
* @param request
* @return
* @throws IOException
*/
@ResponseBody
@RequestMapping("upload")
public String upload(@RequestParam("file")MultipartFile[] file, HttpServletRequest request) throws IOException {
String path = path(request);
System.out.println("\n图片上传中\n");
for (int i = 0; i < file.length; i++) {
File file1 = new File(path + file[i].getOriginalFilename());
if (!file1.getParentFile().exists()){
file1.getParentFile().mkdir();
}
file[i].transferTo(file1);
}
return file[0].getOriginalFilename();
}
/**
* 路径获取
* @param request
* @return
*/
public String path(HttpServletRequest request){
ServletContext servletContext = request.getServletContext();
String realPath = servletContext.getRealPath("/upload/");
return realPath;
}