I have a simple SpringBoot application file upload function, where the maximum file size for downloading files is 2 MB.
I configured multipart.max-file-size=2MB , it works fine. But when I try to upload files larger than 2 MB, I want to handle this error and show an error message.
To do this, my controller implements HandlerExceptionResolver with the implementation of resolveException() as follows:
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) { Map<String, Object> model = new HashMap<String, Object>(); if (exception instanceof MaxUploadSizeExceededException) { model.put("msg", exception.getMessage()); } else { model.put("msg", "Unexpected error: " + exception.getMessage()); } return new ModelAndView("homepage", model); }
The problem is that Exception Im get MultipartException instead of MaxUploadSizeExceededException .
Stacktrace element: Unable to parse a multi- hearted servlet request; The nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase $ FileSizeLimitExceededException: myFile field exceeds the maximum allowed size of 2097152 bytes.
If the file is oversized, why not get a MaxUploadSizeExceededException ? I get its parent MultipartException , which can occur for many other reasons in addition to file size.
Any thoughts on this?
java spring-boot spring-mvc
K. Siva Prasad Reddy
source share