Results 1 to 3 of 3

Thread: Handling MaxUploadSizeExceededException in Spring

  1. #1
    Join Date
    Jun 2012
    Posts
    13

    Default Handling MaxUploadSizeExceededException in Spring

    I'm using Spring 3.2.0. I have implemented the resolveException() method in my annotated controller which implements the HandlerExceptionResolver interface such as,

    Code:
    @Controller
    public final class ProductImageController implements HandlerExceptionResolver
    {
          public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object    handler, Exception exception)
          {
              Map<String, Object> model = new HashMap<String, Object>(0);
              if(exception instanceof MaxUploadSizeExceededException)
              {
                   model.put("msg", exception.toString());
                   model.put("status", "-1");
              }
              else
              {
                   model.put("msg", "Unexpected error : "+exception.toString());
                   model.put("status", "-1");
              }
              return new ModelAndView("admin_side/ProductImage");
         }
    }
    and the Spring configuration includes,

    Code:
    <bean id="filterMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize">
            <value>10000</value>
        </property>
    </bean>
    as specified here.

    When the file size exceeds, the method in the controller class should be invoked and it should automatically handle the exception but it doesn't happen at all. What I'm getting is the full stack trace on the browser. The method resolveException() is never called even though the exception occurs. What is the way to handle this exception? Am I missing something?

    I have also tried registering a separate bean annotated with @Component in application-context.xml but nothing worked.
    Last edited by Tiny; Jan 13th, 2013 at 03:08 PM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    The problem is the exception occurs outside of the flow which is invoked for a controller so in this case the HandlerExceptionResolver isn't going to do much (the name indicates this HandlerExceptionResolver handles exceptions occuring in the handler not before or after the handler execution).

    Also not sure why you have a @Controller implementing that interface (I strongly suggest a read of the reference guide).

    You can defer the multipart parsing (set resolveLazily to true on the CommonsMultipartResolver) this might make the exception occur during injection of the parameters on the method which might make it part of the request handling flow. However that depends on your configuration and which HandlerAdapter you use).

    2 other ways are rely on the exception handling of the servlet container or in other words specify error-pages in the servlet container or extend the DispatcherServlet.
    Last edited by Marten Deinum; Jan 13th, 2013 at 10:08 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jun 2012
    Posts
    13

    Default

    Setting resolveLazily to true unfortunately didn't work. I'm yet not able to display the form when this exception occurs and a user-friendly error message by handling this exception (which should be on the same form). I'm getting the full stack trace on the page.

    Code:
    <bean id="filterMultipartResolver"
              p:resolveLazily="true"
              p:maxUploadSize="10000"
              class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
    Last edited by Tiny; Jan 13th, 2013 at 12:32 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •