Main Tutorials

Spring MVC – How to handle max upload size exceeded exception

In Spring, you can declare a @ControllerAdvice to catch the ugly max upload size exceeded exception like this :

spring-file-upload-max-file-size

Solution

Depends the types of multipartResolver :

  1. StandardServletMultipartResolver – catch MultipartException, refer to this example.
  2. CommonsMultipartResolver – catch MaxUploadSizeExceededException – refer to this example.
GlobalExceptionHandler.java

package com.mkyong.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@ControllerAdvice
public class GlobalExceptionHandler {

    //StandardServletMultipartResolver
    @ExceptionHandler(MultipartException.class)
    public String handleError1(MultipartException e, RedirectAttributes redirectAttributes) {

        redirectAttributes.addFlashAttribute("message", e.getCause().getMessage());
        return "redirect:/uploadStatus";

    }

    //CommonsMultipartResolver
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public String handleError2(MaxUploadSizeExceededException e, RedirectAttributes redirectAttributes) {

        redirectAttributes.addFlashAttribute("message", e.getCause().getMessage());
        return "redirect:/uploadStatus";

    }

}
Tomcat Connection Reset
If you deployed to Tomcat, and unable to catch the file size exceeded exception, this may cause by the Tomcat maxSwallowSize setting. Read this – Spring file upload and connection reset issue

References

  1. Spring’s multipart (file upload) support

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Barak
6 years ago

At my client application, I was able getting catching the error with @ExceptionHandler
But inspecting the network status I get:
ERR_CONNECTION_ABORTED
I have tried having set status for httpServletResponse – but the status is always ERR_CONNECTION_ABORTED.

How can I get a different status and the Network, the stack trace is not good enough

Barak
6 years ago

I have created a class called “CORSFilter extends OncePerRequestFilter”
and it is annotated with @ControllerAdvice
I have tried uploading a large file – in the stack trace I do get this error

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size of 41943040 bytes exceeded; nested exception is org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (53990195) exceeds the configured maximum (41943040)

but the Debugger doesn’t stop at @ExceptionHandler method

Driuki
1 year ago
@ExceptionHandler({MaxUploadSizeExceededException.class, SizeLimitExceededException.class})
public String handleFileUploadError(RedirectAttributes redirectAttrib) {
    redirectAttrib.addFlashAttribute("error", "File is bigger than "+ fileSize +" MB");
    System.out.println("1");
    return "redirect:/listing-settings";
}

I have this code and it gets called 7 times, but it doesn’t seem to redirect me to page that I whant to and it looks like it does not add anything to RedirectAttributes.

Михаил Лысаков
3 years ago

Some browsers dont close connection when error reseived. In my case i close it manyally:

  @ExceptionHandler(MaxUploadSizeExceededException.class)
  public ResponseEntity<String> Exception(MaxUploadSizeExceededException e, RedirectAttributes redirectAttributes) {
    return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
        .contentType(MediaType.TEXT_HTML)
        .header(HttpHeaders.CONNECTION, "close")
        .body("ERROR:" + e.getMessage());
  }
Last edited 3 years ago by Михаил Лысаков
Михаил Лысаков
3 years ago

At this sample connection reset error can occur on Jetty when ExceptionHandler used. ExceptionHandler must be annoteted @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) (Tested on jetty-9.4.15)

Olaf Schmidt
4 years ago

This solution saved my day. Couldn’t make any other solution work.

Your tutorials are alway very helpful. Thanks, for all the efforts you put into your coding examples and tutorials!