1、问题:今天在spring boot 上传31M的文件,报下面的错误
org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field apkFile exceeds its maximum permitted size of 31457280 bytes.
at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl$FileItemStreamImpl$1.raiseError(FileUploadBase.java:630) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.checkLimit(LimitedInputStream.java:76) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:135) ~[tomcat-embed-core-8.5.28.jar:8.5.28]
at java.io.FilterInputStream.read(FilterInputStream.java:107) ~[na:1.8.0_161]
2、原因:
因为spring boot 默认对上传进行了限制。我们上传29M的时候,是没有问题的。
3、修改方式:
1)配置文件 application.properties 增加如下内容:
spring.servlet.multipart.enabled =true
spring.servlet.multipart.file-size-threshold =0
spring.servlet.multipart.max-file-size=100Mb
spring.servlet.multipart.max-request-size=100Mb
2) 启动类,增加如下代码
@SpringBootApplication
@EnableCaching
@EnableTransactionManagement
public class ManagerApplication {
public static void main(String[] args) {
SpringApplication.run(ManagerApplication.class, args);
}
/**
* 配置上传文件大小的配置
* @return
*/
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
// 单个数据大小
factory.setMaxFileSize("102400KB");
/// 总上传数据大小
factory.setMaxRequestSize("102400KB");
return factory.createMultipartConfig();
}
}
设置完成后,上传没有问题