I have simple SpringBoot app file uploading functionality where max file upload file size is 2 MB.
I have configured multipart.max-file-size=2MB
it is working fine.
But when I try to upload files with larger than 2 MB size I want to handle that error and show the error message.
For that I have my controller implements HandlerExceptionResolver
with resolveException()
implementation 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 the Exception Im getting is MultipartException instead of MaxUploadSizeExceededException.
The stacktrace is: Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field myFile exceeds its maximum permitted size of 2097152 bytes.
In the case of file size exceeds why not I am getting MaxUploadSizeExceededException? I am getting its parent Exception MultipartException which can be occured for many other reasons in addition to File Size exceeds.
Any thoughts on this?