Multipage file maximum file size exception - spring uploaded tomcat file

I set the maximum file size

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 error message

 2016-03-03 13:48:24.560 WARN 4992 --- [nio-8080-exec-1] hcwRestResponseEntityExceptionHandler : 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] hcwRestResponseEntityExceptionHandler : 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 the final result after the request with a large file is the problem loading page. I am not getting any other error in the stack trace, so I am a little fixated on what is actually happening. Oh yes, I tried many other solutions, such as registering a filter, handling exceptions in ErrorController. Every time I get the same result, the server crashes. Tomcat 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); } 

}

+5
source share
3 answers

It was complicated. This issue caused the MaxSwallowSize property from Tomcat. Apparently, it was introduced in one of the latest versions of Tomcat. The whole idea was that Tomcat realized that the request would be rejected in order to terminate the connection with something higher than the standard 2mb (at least that was my interpretation). A simple override of this property captures everything. I understand that this is not an ideal solution, but it is much better than just terminating the 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; } 
+10
source

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

See spring download guides

+10
source

I wrote a few lines in application.yml to solve this problem, for example:

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

This helped when I wrote in application.properties, but not in yml.

0
source

Source: https://habr.com/ru/post/1244296/


All Articles