Skip to Content
0
Jan 17, 2020 at 10:12 AM

How to handle the MaxUploadSizeExceededException on Java Web Tomcat 8 runtime (NEO)?

702 Views Last edit Jan 17, 2020 at 03:30 PM 3 rev

Hi,

We have a Spring based Java Web Tomcat 8 application hosted on SAP Cloud Platform.

We have configured the max upload size to 120 MB and also the max in-memory upload size to 120 MB as in the following code snippet:

@Configuration
public class CommonsMultipartResolverConfig {

    private static final int MAX_IN_MEMORY_UPLOAD = 120 * 1024 * 1024;
    private static final int MAX_UPLOAD_SIZE = 120 * 1024 * 1024;

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
    @Lazy(true)
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        commonsMultipartResolver.setMaxUploadSize(MAX_UPLOAD_SIZE);
        commonsMultipartResolver.setMaxInMemorySize(MAX_IN_MEMORY_UPLOAD);
        return commonsMultipartResolver;
    }}

All uploads work fine while the files are less or equal to the 120 MB threshold. When the size exceeds it, on the client side we end-up with the following response:

<html> <head> <title>Error report</title> </head> <body> <h1>HTTP Status 500 - An internal application error occurred. Request: 3821930456 a3f704cbf:rsmwebui</h1> </body> </html>

On the Java app we have also set a custom handling for the MaxUploadSizeExceededException:

@ControllerAdvice
public class GeneralExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ResponseEntity<ErrorResponse> handleMaxSizeException(MaxUploadSizeExceededException ex) {
        long maxSizeInMB = ex.getMaxUploadSize() / 1024 / 1024;
        ErrorResponse err = ErrorResponse.builder().message("Maximum upload size of " + maxSizeInMB + " MB exceeded").build();
        return new ResponseEntity<>(err, HttpStatus.PAYLOAD_TOO_LARGE);
    }
}

Despite the above code being triggered, the response is always the above html.

From the http logs at the java application level, we also noticed that when uploading the file that exceeds the limit, there are multiple http calls being logged in consequence:

I've read that this might be due to the connection being reset by the Tomcat server in consequence to its own connector settings (maxPostSize, maxSwallowSize).

Please advise on how could we manage to have a proper customised message being received on the client side, which actually informs about the limit that has been exceeded.

Thanks!

Attachments