springboot上傳檔案超限BUG
[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 files exceeds its maximum permitted size of 1048576 bytes.] to ModelAndView: reference to view with name '/error'; model is {exception=Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field files exceeds its maximum permitted size of 1048576 bytes.}
大概意思就是說,上傳的檔案太大了,超過自身限制了
我在Controller裡面還加了限制,打斷點並沒有走到這裡來
if (file.isEmpty()) {
return Result.error("檔案為空!");
}
// 上傳檔案
if (file.getSize() > 5 * 1024 * 1024) {
return Result.error("檔案大小超出範圍!");
}
The field files exceeds its maximum permitted size of 1048576 bytes.
1024*1024 = 1048576 = 1M
經過各種試驗得出spring boot 預設的上傳檔案大小是1024b*1024b
實現方式1: 在spring boot的啟動類中加入下面配置
/** * 檔案上傳配置 * @return */ @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); //單個檔案最大 5M factory.setMaxFileSize("5120KB"); //KB,MB /// 設定總上傳資料總大小 factory.setMaxRequestSize("102400KB"); return factory.createMultipartConfig(); }
實現方式2:在配置檔案中加入配置
spring:
servlet:
multipart:
max-file-size: 2Mb
max-request-size: 2Mb
再測試一下就ok了
spring boot 2.0環境,之前的配置稍微有一點不同