cancel
Showing results for 
Search instead for 
Did you mean: 

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

0 Kudos

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!

Accepted Solutions (0)

Answers (1)

Answers (1)

Ivan-Mirisola
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi flaviu.pop,

In order for a JavaBuildPack yo take specific Tomcat configurations into account, you have two options as described in the following document:

https://community.pivotal.io/s/article/How-to-Customize-Java-Build-packs-Tomcat-s-Server-xml

I'm not saying that this should change the behavior you are having with the error 500. But it is worth a try. Please let us know if it indeed helped.

Best regards,
Ivan

0 Kudos

Hi Ivan,

Thanks for your response!

The proposed solution actually applies for the Cloud Foundry deployments.

As specified in the title, we are in NEO context.

Do you have any suggestion for this setup?

Thanks & regards,

Flaviu

Ivan-Mirisola
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi flaviu.pop,

I'm sorry, I didn't notice the title of your question.

In the case of Neo, I'm not aware of any options to modify the standard tomcat configuration. It is probably not supported, since you can only control the Java startup arguments while deploying the war file.

However, it might be possible to configure the tomcat server via Spring as explained below:

https://mkyong.com/spring-boot/spring-boot-configure-maxswallowsize-in-embedded-tomcat/

Best regards,
Ivan