背景
MultipartFile 上傳文件出錯
public String uploadImg(@RequestParam("file") MultipartFile file, boolean flag) {
String filePicUrl = "";
String id ="";
File files = null;
files = FileUtil.createFiles(prefix +filePicUrl, id+".jpg");
file.transferTo(files);
}
報錯
2020-05-22 21:06:24.236 [http-nio-5000-exec-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Could not complete request
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.handleParseFailure(StandardMultipartHttpServletRequest.java:120)
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:113)
at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.<init>(StandardMultipartHttpServletRequest.java:86)
at org.springframework.web.multipart.support.StandardServletMultipartResolver.resolveMultipart(StandardServletMultipartResolver.java:93)
at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1128)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:960)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
原因
The field file exceeds its maximum permitted size of 1048576 bytes
錯誤顯示文件的大小超出了允許的範圍
查詢發現Spring Boot做了文件傳輸大小限制,可在配置中修改其默認值。
解決方案
在spring配置文件中加入如下配置:
#修改上傳文件無限制大小
spring.servlet.multipart.max-file-size = -1
spring.servlet.multipart.max-request-size= -1