In the previous post we have seen sample code of Spring MVC File Upload Example.
Now in this post we will see the code for Spring MVC multiple File upload.
In this we will use a Model class to store a list of MultpartFile objects. The list will be passed from the view to the Controller for processing.
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. Model Class to Store the File Object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.kscodes.sampleproject.model; import java.util.List; import org.springframework.web.multipart.MultipartFile; public class MultiFileUploadBean { private List<MultipartFile> uploadedFiles; public List<MultipartFile> getUploadedFiles() { return uploadedFiles; } public void setUploadedFiles(List<MultipartFile> uploadedFiles) { this.uploadedFiles = uploadedFiles; } } |
4. View JSP to get all the files selected by User.
This is a HTML form. Please note that we have used enctype=”multipart/form-data” so that file objects can be passed as is to the Controller.
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 |
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring MVC - Multiple File Upload Sample</title> </head> <body> <h2>Multiple File Upload</h2> <form:form commandName="multiFileUploadBean" method="post" action="uploadMultipleFile" enctype="multipart/form-data"> <table> <tr> <td>Please select Files to upload:</td> </tr> <tr> <td> </td> </tr> <tr> <td><input type="file" name="uploadedFiles[0]"></td> </tr> <tr> <td><input type="file" name="uploadedFiles[1]"></td> </tr> <tr> <td><input type="file" name="uploadedFiles[2]"></td> </tr> <tr> <td> </td> </tr> <tr> <td><input type="submit" value="Upload" /></td> </tr> </table> </form:form> </body> </html> |
5. Controller to process the list of files
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package com.kscodes.sampleproject.controller; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; import com.kscodes.sampleproject.model.MultiFileUploadBean; @Controller public class FileUploadController { @RequestMapping(value = "/filehandling") public String handleFile() { return "fileUpload"; } @RequestMapping(value = "/uploadMultipleFile", method = RequestMethod.POST) public ModelAndView uploadMultipleFile(@ModelAttribute MultiFileUploadBean multiFileUploadBean) { String returnPage = null; ModelAndView modelAndView = new ModelAndView(); if (multiFileUploadBean != null) { returnPage = "success"; // 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. // For now we will get the list of file names and display it on // success page List<String> uploadedFileNames = new ArrayList<String>(); if (multiFileUploadBean.getUploadedFiles() != null) { for (MultipartFile file : multiFileUploadBean.getUploadedFiles()) { uploadedFileNames.add(file.getOriginalFilename()); } modelAndView.addObject("listUploadedFiles", uploadedFileNames); } } else { returnPage = "error.jsp"; } modelAndView.setViewName(returnPage); return modelAndView; } } |
6. Success page to display the list of Files that we had passed to the Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<%@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>Multiple File Upload :: Success !!!</h2> <h3>We have successfully uploaded the following files </h3> <table> <c:forEach items="${listUploadedFiles}" var="fileName"> <tr><td>${fileName}</td></tr> </c:forEach> </table> </body> </html> |