In one of the earlier posts we have seen Spring MVC File Upload Example. What happens if you have a specific requirement of not allowing users to upload file of over a specified limit? In simple apps, you may end up getting the file size and then may be throwing an exception if file size goes above limit. Things are easier in Spring. We have a property that helps define you a limit of max size for file upload.
Lets see an example of Spring MVC File Upload Max Size Validation
1. Dispatcher Servlet Configuration for Max upload property
In the CommonsMultipartResolver bean we add a property maxUploadSize and specify the maximum allowed limit in bytes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com.kscodes.sampleproject" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="1000" /> </bean> <mvc:annotation-driven /> </beans> |
2. Controller for File Upload
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package com.kscodes.sampleproject.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; @Controller public class FileUploadController { @RequestMapping(value = "/fileUpload") public String handleFile() { return "fileUpload"; } @RequestMapping(value = "/uploadSingleFile", method = RequestMethod.POST) public ModelAndView uploadFile(@RequestParam("file") MultipartFile file) { String returnPage = null; ModelAndView modelAndView = new ModelAndView(); if (file != null) { returnPage = "success"; modelAndView.addObject("fileName", file.getOriginalFilename()); // Once you have the file object, you can perform the required // operations on that. E.g Storing the file, validating it or // sending it to someone else. } else { returnPage = "error.jsp"; } modelAndView.setViewName(returnPage); return modelAndView; } } |
3. Exception Handler for Max Upload exception
We will add a Exception handler that will catch the MultipartException when a File above limits is uploaded. In the handleFileUploadException method you can add your own implementation on how you want to handle the exception. I have added the exception message to a Model and displayed it on the Error page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.kscodes.sampleproject.exception; import javax.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.multipart.MultipartException; import org.springframework.web.servlet.ModelAndView; @ControllerAdvice public class GenericExceptionHandler { @ExceptionHandler(value = MultipartException.class) public ModelAndView handleFileUploadException(MultipartException mpex, HttpServletRequest request) { ModelAndView modelAndVew = new ModelAndView("error"); modelAndVew.addObject("errorMsg", mpex.getMessage()); return modelAndVew; } @ExceptionHandler(value = Exception.class) public ModelAndView handleException(Exception ex, HttpServletRequest request) { ModelAndView modelAndVew = new ModelAndView("error"); modelAndVew.addObject("errorMsg", ex.getMessage()); return modelAndVew; } } |