Tomcat异常,post提交超过大小上限被tomcat拦截

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等也会对文件上传做一定的限制,具体问题具体分析。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值