File Upload in Spring MVC

Spring MVC version 3 brings a lot of functionality that makes file upload a breeze. However, there are a number of steps you have to take care of in order to ensure this functionality works.

The details: you want to use google chrome that has the opportunity to use the multiple attribute on the html file upload field. So there will be a number of files that are uploaded under the same. The container is Tomcat version 8 and the library is Spring MVC 3.

The steps:

  1. Container maximum size
  2. Spring MVC multipart configuration
  3. The JSP
  4. Spring MCN controller

Container Maximum Size

Ensure the container supports the upload size you need. Most of the containers support by default 2Mb and in today's world this size is nothing. Here is what I have for tomcat:

  1.  
  2. <Connector executor="tomcatThreadPool"
  3. port="8080" protocol="HTTP/1.1"
  4. connectionTimeout="20000"
  5. redirectPort="8443" maxPostSize="0" />
  6.  

maxPostSize zero removes the size restriction which in my case is the requirement.

Spring MVC multipart configuration and maximum size

In the xml associated with the dispacher servlet where you configure the beans, add the setting for the multipart resolver. It is very important not only to add the setting but also to ensure you adequately set the value for the maximum upload size. This is of course different than the container one:

  1.  
  2. <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
  3.  
  4. <property name="maxUploadSize" value="2000000000" />
  5. <property name="maxInMemorySize" value="1048576" />
  6.  
  7. </bean>
  8.  

In this case zero will no longer do it - you have to specifically come up with a positive value. Another thing, note the maxInMemorySize thing. Don't choose it to be too big. As soon as the multipart size associated with a file goes in excess of this value, will be spooled on the disk. So if you upload a file with a size of half a gig, a low setting you will ensure the tomcat immediately spool it on the disk and the processing will be successful even if the application server runs in maximum 300 megs of heap memory.

The JSP

Here is the jsp, a typical short jsp file that has the tag together with the file upload one. As there is no specific spring mvc tag for file upload, the one from the html array will have to do.

  1.  
  2. <form:form action="${pageContext.request.contextPath}/upload.html" enctype="multipart/form-data">
  3. <h3>Upload Transactions</h3>
  4. <table class="form">
  5. <tr>
  6. <td class="middle">File(s):</td>
  7. <td><input type="file" name="flupload" multiple="multiple"></td>
  8. </tr>
  9. </table>
  10.  
  11. <div>
  12. <input type="submit" name="trigger" value="Submit">
  13. </div>
  14. </form:form>
  15.  

Note the "multiple" attribute. This works with chrome and not sure if it does with other browser, however for us, its use was the requirement.

The Spring MVC controller

Of course the controller involved here must be rigged to adeuqately process the file. Here, Spring MVC makes the life very easy. See above, in the jsp, the name of the file upload field is "fileupload". It is very simple to declare and then process it in the controller, as below:

  1.  
  2. @RequestMapping(value="/upload", method=RequestMethod.POST)
  3. public ModelAndView upload(@RequestParam("flupload") MultipartFile[] files){
  4. System.out.println("ok, the number of files that were provided is: " + files.length);
  5.  
  6. return new ModelAndView("login", "command", new LoginModel());
  7. }
  8.  

The key thing is @RequestParam("flupload") MultipartFile[] files

  • There is a request parameter called flupload
  • It maps to an array, to provide support for the multiple file upload

That's it. It is simple to do, but skip any of the steps above, especially the ones related to the maximum size and you start having issues.