views:

91

answers:

1

Hi,

I know its hard to check the file size at the client side(browser) with just pure javascript only.

Now, my question is, Is there a way at the server side to catch an exception such as this?

org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size of 2000000 bytes

What happens is that, it does not reach my @controller post method and it just throws up the exception that is being catch up by my error.jsp.

What I was thinking is that, is it possible to do this in spring mvc annotated method?

@RequestMapping("/uploadFile.htm")
    public String uploadAttachment(
        HttpServletRequest request,
        @RequestParam(required = false, value = "attached-file") MultipartFile file,
        ModelMap model) throws Exception {

        if(checkfilesize(file)){
            //populate model
            //add error if appplicable
            //return same form again
        }
        //return success
    }       
}

My problem is, it doesnt reaches upto this point and just throw up a big fat exception.

Although the error.jsp was able to catch it, I would think its much user friendly if I can alert the user that the file they are about to upload exceeds the limit.

This is Spring MVC 2.5 app by the way. Is this possible?

+1  A: 

This exception is thrown in DispatcherServlet.doDispatch(), so you should be able to catch this using a HandlerExceptionResolver configured in your context.

skaffman
@Skaffman, thanks.. I made some reading about this and I am using it now. My problem is, how can I show the same form again populated with the same model and form:error objects. When I do a forward to the same form again, no data is populated.
Mark Estrada