Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I handle Tomcat's MaxUploadSizeExceededException in Spring?

I have done some research around this with conflicting results. To handle this error, some say that I need to implement HandlerExceptionResolver in one of my controllers.

Here are some links for that:

  • How to handle MaxUploadSizeExceededException
  • Handling MaxUploadSizeExceededException with Spring MVC
  • http://www.raistudies.com/spring/spring-mvc/file-upload-spring-mvc-annotation/

On the other hand, other people are saying that this approach is futile such that the Exception is occuring outside the request handling flow:

  • http://forum.spring.io/forum/spring-projects/web/124409-handling-maxuploadsizeexceededexception-in-spring (The second poster in the thread)
  • MaxUploadSizeExceededException doesn't invoke the exception handling method in Spring

I have tried the above solutions but they do not work for me. It appears that the Exception occurs outside of Spring, as expected. I am unable to catch this even with HandlerExceptionResolver.

like image 334
Johnathan Au Avatar asked Feb 23 '15 22:02

Johnathan Au


People also ask

How does spring boot handle MaxUploadSizeExceededException?

Handling MaxUploadSizeExceededException In order to handle this exception, we can have our controller implement the interface HandlerExceptionResolver, or we can create a @ControllerAdvice annotated class.

How do I get the size of a multipart file in MB?

You are using method getSize() of MultipartFile, which I think returns size in bytes in binary. So convert that to in MB you have to multiply file. getSize() with 0.00000095367432 . This will give you size in MB.

What is multipart Max request size?

The default is 1MB. max-request-size specifies the maximum size allowed for multipart/form-data requests. The default is 10MB. file-size-threshold specifies the size threshold after which files will be written to disk.


1 Answers

Trying following the approach specified in the link below. Basically, you configure an error page for any un handled exception and then define a handler for the error page. Looks like a decent workaround.

Here is the link http://www.javacodegeeks.com/2013/11/how-to-custom-error-pages-in-tomcat-with-spring-mvc.html

Hope this helps.

like image 140
Nitin Arora Avatar answered Nov 08 '22 08:11

Nitin Arora