Spring MVC File upload is made easy by the Apache Commons FileUpload dependency. Lets see the steps on how we can use that in the sample code for Spring MVC File Upload.
1. Dependencies in pom.xml
Add the following dependencies of Apache Commons in your pom.xml
1 2 3 4 5 6 7 8 9 10 11 |
<dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.4</version> </dependency> |
2. MultipartResolver configuration
Create bean of the CommonsMultipartResolver in your dispacther-servlet.xml
1 2 |
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /> |
3. Form to get the File
Create a View to get the file from User and then submit it to the Controller.
The important point to note is that form enctype should be multipart/form-data, so that Spring web application knows that the request to the controller contains file data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring MVC - Single File Upload Sample</title> </head> <body> <h2>Single File Upload</h2> <form:form method="post" action="uploadSingleFile" enctype="multipart/form-data"> <table> <tr> <td>Please select a File to upload:</td> <td><input type="file" name="file"></td> </tr> <tr> <td colspan="2"><input type="submit" value="Upload" /></td> </tr> </table> </form:form> </body> </html> |
4. File Upload Controller
Add a Controller to process the file that was sent using enctype=”multipart/form-data”
Please note that the param to the uploadFile method is a MultipartFile .
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 = "/filehandling") 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; } } |
5. Success page
Finally after you are done with processing of the file show a success or error page.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <head> <title>Spring MVC - Single File Upload Sample</title> </head> <body> <h2>Single File Upload :: Success !!!</h2> We have successfully uploaded the file - <strong> ${fileName} </strong>. </body> </html> |