多部分文件最大大小异常 - spring boot embbeded tomcat

时间:2016-03-02 13:02:59

标签: spring spring-boot multipartform-data multipart embedded-tomcat-8

我已将最大文件大小设置为

multipart.maxFileSize: 1mb
multipart.maxRequestSize: 1mb

这是我的控制者:

@RequestMapping(method=RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseStatus(HttpStatus.CREATED)
@Secured(Privileges.CAN_USER_READ)
public void create(@RequestParam("file")final MultipartFile file,Principal principal) throws IllegalStateException, IOException,MultipartException{

    medicalHistoryService.create(new MedicalHistory(file));
}

这是错误消息

2016-03-03 13:48:24.560  WARN 4992 --- [nio-8080-exec-1] h.c.w.RestResponseEntityExceptionHandler : 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 (9288401) exceeds the configured maximum (1048576)

2016-03-03 13:48:25.545  WARN 4992 --- [nio-8080-exec-2] h.c.w.RestResponseEntityExceptionHandler : 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 (9288401) exceeds the configured maximum (1048576)

请求超大文件后的最终结果是问题加载页面。我没有在堆栈跟踪中得到任何其他错误,所以我有点坚持实际发生的事情。哦,是的,我已经尝试了许多其他解决方案,如注册过滤器,在ErrorController中处理异常。每次我都会得到相同的结果 - 服务器崩溃。Tomcat crash

编辑2

我的异常处理类:

@ControllerAdvice
public class RestResponseEntityExceptionHandler extends 
ResponseEntityExceptionHandler{

// 413 MultipartException - file size too big 
@ExceptionHandler({MultipartException.class,FileSizeLimitExceededException.class,java.lang.IllegalStateException.class})
public ResponseEntity<Object> handleSizeExceededException(final WebRequest request, final MultipartException ex) {
    //log.warn("413 Status Code. File size too large {}", ex.getMessage());
    log.warn(ex.getMessage());
    final ApiError apiError = message(HttpStatus.PAYLOAD_TOO_LARGE, ex);
    return handleExceptionInternal(ex, apiError, new HttpHeaders(), HttpStatus.PAYLOAD_TOO_LARGE, request);
}

}

7 个答案:

答案 0 :(得分:15)

从Spring Boot 2开始

spring.servlet.multipart.max-file-size=128KB
spring.servlet.multipart.max-request-size=128KB

请参阅docs

Spring Boot 1.x

属性应该如下:
spring.http.multipart.max-file-size=128KB
spring.http.multipart.max-request-size=128KB

请参阅spring boot guides

答案 1 :(得分:11)

这很棘手。 Tomcat属性MaxSwallowSize导致了这个问题。显然它是在最新版本的Tomcat之一中引入的。它背后的整个想法是,如果Tomcat意识到请求将被拒绝,终止高于默认2mb的连接(至少这是我的解释)。简单覆盖此属性可修复问题。我意识到这不是完美的解决方案,但它比终止连接要好得多。

@Bean
public TomcatEmbeddedServletContainerFactory containerFactory() {
    TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
     factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
        @Override
        public void customize(Connector connector) {
         ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
        }
     });
     return factory;
}

答案 2 :(得分:3)

由于@SeaBiscuit提供了正确的答案,但是使用了Spring Boot 2.0.0.RELEASEorg.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory已被删除,并由类org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory取代。 因此,新的Spring Boot 2.0.0的代码应为

  1. 用任何名称创建一个类,并用@Configuration进行注释
  2. 并将以下代码放入该类中
@Bean
public TomcatServletWebServerFactory containerFactory() {
    TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
    factory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
        @Override
        public void customize(Connector connector) {
            ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
        }
    });
    return factory;
}

如果您在Spring Boot 2.0.0中配置最大文件上传大小时遇到​​麻烦 将以下代码放在“ application.propertise”文件中,并以“ MB”表示所需的文件大小

## Image upload limitation properties
spring.servlet.multipart.max-file-size=3MB
spring.servlet.multipart.max-request-size=3MB

答案 3 :(得分:2)

请在 application.properties 中添加以下行以获取春季启动版本- 2.0.1.RELEASE

spring.servlet.multipart.max-file-size=128MB
spring.servlet.multipart.max-request-size=128MB
spring.servlet.multipart.enabled=true

这解决了我的问题。

答案 4 :(得分:1)

我在application.yml中写了一些代码来解决这个问题:

spring:
    http:
        multipart:
            max-file-size: 10MB
            max-request-size: 10MB

我在application.properties中编写时帮助了,但在yml中却没有。

答案 5 :(得分:1)

我尝试了上述所有建议,但在添加此之前仍然出现错误:

  server.tomcat.max-swallow-size=-1

在 application.properties 文件中。

答案 6 :(得分:0)

以下代码对我有用。将其添加到spring boot主类:

import javax.servlet.MultipartConfigElement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;



@SpringBootApplication
@EnableScheduling
@EnableAsync
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize("5MB");
        factory.setMaxRequestSize("5MB");
        return factory.createMultipartConfig();
    }
}