Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle SizeLimitExceededException from CommonsMultipartResolver in Spring WebFlow?

I have the following situation. I have a CommonsMultipartResolver bean configured the following way.

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="2100000" />

And I have a few file upload fields in a Spring Web Flow view state jsp.

Everything works fine if the file is under the limit, but if the file exceeds the limit of 2MB-s I have to add a validation error to the binding result on my form.

My problem is that the multipart file resolver throws a org.apache.commons.fileupload.FileUploadBase.SizeL imitExceededException exception when the file limit is exceeded and I can't find a way to catch this in Spring Web Flow and add my FieldError to the form.

I tried using the on-exception attribute of the transition tag, but if I understand correctly it only works for exceptions that are thrown within Spring Web Flow.

I've also tried to use SimpleMappingExceptionResolver in spring mvc, but I do not want to redirect to a page, I want to handle this exception.

I also found this: https://jira.springsource.org/browse/SWF-158

But it's from version 1.0 and I'm assuming that this has been incorporated since or that a better way was found to handle these situations.

Any ideas on how to deal with this would be greatly appreciated.

Thanks.

like image 794
Balázs Béla Avatar asked Oct 29 '12 07:10

Balázs Béla


People also ask

What is maxuploadsizeexceededexception in Spring Boot?

In the Spring framework, a MaxUploadSizeExceededException is thrown when an application attempts to upload a file whose size exceeds a certain threshold as specified in the configuration. In this tutorial, we will take a look at how to specify a maximum upload size.

How do I use commonsmultipartresolver in spring?

We must add the Apache Commons File Upload dependency (commons-fileupload.jar) in order to use CommonsMultipartResolver. Spring by default will not handle multipart file uploads, however it provides the support to multipart using the pluggable multipart object called “MultipartResolver”.

How do I upload multiple files in Spring MVC?

In this Spring MVC Multiple File Upload Example, we will learn how to upload multiple files in Spring MVC using CommonsMultipartResolver. We must add the Apache Commons File Upload dependency (commons-fileupload.jar) in order to use CommonsMultipartResolver.

What is commonsmultipartresolver in Spring Boot?

Whenever the DispatcherServlet detects a multipart request, it activates the CommonsMultipartResolver which is declared in the Spring Context. The MultipartResolver then wraps the plain HttpServletRequest into a MultipartHttpServletRequest which has support for multipart file uploads.


1 Answers

In your SimpleMappingExceptionResolver you should be able to override the resolveException method, determine the exception type being caught and handle appropriately.

I've found some old code in our project that seems to be a solution to a similar exception;

public class GeneralMappingExceptionResolver extends SimpleMappingExceptionResolver {

 @Override
 public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) {

    if(exception instanceof MaxUploadSizeExceededException) {
        MaxUploadSizeExceededException maxe = (MaxUploadSizeExceededException)exception;
        String errorMessage = "Max filesize exceeded, please ensure filesize is too large.");
        HashMap<String, Object> model = new HashMap<String, Object>(2);
        model.put("errorMessage", errorMessage);
        return new ModelAndView("verification/psv/consent", model);
    } else {
        return super.resolveException(request, response, handler, exception); // Do whatever default behaviour is (ie throw to error page).
    }
}

Note that the "verification/psv/consent" is the flow where this exception would have been thrown from and where it needs to return to. We only have the one page that has a file upload.

Obviously the errorMessage is just a parameter passed into the view so will need to be handled and displayed like an error message. You may also need to re-populate any other form fields that were submitted. Hopefully this is a point in the right direction though.

like image 106
Nick Foote Avatar answered Jan 02 '23 20:01

Nick Foote