-->

Multipart file maximum size exception - spring boo

2020-03-01 18:24发布

问题:

I have set max file size to

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

This is my controller :

@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));
}

this is error message

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)

And final result after request with over-sized file is problem loading page. I dont get any other error in stack trace so i am kinda stuck with what is actually going on. Oh yeah i have tried many other solutions such as registering filter, handling exception in ErrorController. Every time i would end up with same result - server crash.

EDIT 2

My exception handling class :

@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);
}

}

回答1:

Started from Spring Boot 2

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

See docs

Spring Boot 1.x

Properties should like:
spring.http.multipart.max-file-size=128KB
spring.http.multipart.max-request-size=128KB

See spring boot guides



回答2:

This was tricky. Tomcat property MaxSwallowSize was causing this problem. Apparently it was introduced in one of the recent versions of Tomcat. The whole idea behind it was if Tomcat realized the request was going to be rejected, to terminate the connection anything higher than default 2mb (at least this was my interpretation). Simple overriding this property fixes things. I realize this is not perfect solution, but it is a whole lot better than just terminating connection.

@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;
}


回答3:

Please add the below lines in application.properties for spring boot version -2.0.1.RELEASE

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

This resolved my issue.



回答4:

Since @SeaBiscuit has provided the correct answer but with Spring boot 2.0.0.RELEASE the class org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory has been removed and replaced by the class org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory. So the code for the new Spring boot 2.0.0 would be

  1. Create a class with any name and annotate it with @Configuration
  2. And put the below code inside that class
@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;
}

And if you've trouble configuring the maximum file upload size in Spring boot 2.0.0 put below code inside 'application.propertise' file with desired file size in 'MB's

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


回答5:

I wrote some lines in application.yml to solve this issue like:

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

It helped while I wrote in application.properties but not in yml.