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

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

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


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:

public class CommonsMultipartResolverConfig {

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

    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        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:

public class GeneralExceptionHandler extends ResponseEntityExceptionHandler {

    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.