Spring MVC How to handle max upload size exceeded exception
By:Roy.LiuLast updated:2019-08-11
In Spring, you can declare a @ControllerAdvice to catch the ugly max upload size exceeded exception like this :
Solution
Depends the types of multipartResolver :
- StandardServletMultipartResolver – catch MultipartException, refer to this example.
- 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
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
From:一号门
COMMENTS