org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (33938635) exceeds the configured maximum (31457280)
第一种:
omcat7.0.63之前:
maxPostSize The maximum size in bytes of the POST which will be handled by the container FORM URL parameter parsing. The limit can be disabled by setting this attribute to a value less than or equal to 0. If not specified, this attribute is set to 2097152 (2 megabytes).
设置为0和负数均可以代表不限制
tomcat7.0.63(包含)之后:
maxPostSize The maximum size in bytes of the POST which will be handled by the container FORM URL parameter parsing. The limit can be disabled by setting this attribute to a value less than zero. If not specified, this attribute is set to 2097152 (2 megabytes).
不可以设置为0,只能是负数代表不限制
由于我修改第一种不起作用:
于是 第二种:
第一种、直接在springboot的启动类里加入下面的代码。
@Configuration
public class TomcatConfig {
@Value("${spring.server.MaxFileSize}")
private String MaxFileSize;
@Value("${spring.server.MaxRequestSize}")
private String MaxRequestSize;
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
// 单个数据大小
factory.setMaxFileSize(MaxFileSize); // KB,MB
/// 总上传数据大小
factory.setMaxRequestSize(MaxRequestSize);
return factory.createMultipartConfig();
}
}
然后再在application.properties中进行配置
#文件请求大小
spring.server.MaxFileSize=300MB
spring.server.MaxRequestSize=500MB
还有一种
spring.http.multipart.maxFileSize=10Mb
spring.http.multipart.maxRequestSize=10Mb
maxFileSize 是单个文件大小
maxRequestSize是设置总上传的数据大小
这就可以了。
注意
--spring.http.multipart.maxFileSize=10Mb spring.http.multipart.maxRequestSize=10Mb--
已经过期 >>>>可以使用
spring.servlet.multipart.maxFileSize=10Mb spring.servlet.multipart.maxRequestSize=10Mb
根据自己需求定义吧,Mb和Kb都可以,大小写也都随意,Long字节应该可以的
另外这是MultipartConfigFactory类中的转换,可以瞅一眼
private long parseSize(String size) {
Assert.hasLength(size, "Size must not be empty");
size = size.toUpperCase();
return size.endsWith("KB")?Long.valueOf(size.substring(0, size.length() - 2)).longValue() * 1024L:(size.endsWith("MB")?Long.valueOf(size.substring(0, size.length() - 2)).longValue() * 1024L * 1024L:Long.valueOf(size).longValue());
}
最后:Tomcat,Nginx等也会对文件上传做一定的限制,具体问题具体分析。