In this simple tutorial we will see how to implement multiple file upload in a Spring 3 MVC based application.
The requirement is simple. We have a form which displays file input component. User selects a file and upload it. Also its possible to add more file input components using Add button. Once the files are selected and uploaded, the file names are displayed on success page.
1. Maven Dependencies / Required JAR files
If you using Maven in your project for dependency management, you’ll need to add dependencies for Apache Common File upload and Apache Common IO libraries. The spring’s CommonsMultipartResolver class internal uses these library to handle uploaded content.
Add following dependencies in your maven based project to add File upload feature.
<dependencies><!-- Spring 3 MVC --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>3.1.2.RELEASE</version></dependency><!-- Apache Commons file upload --><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.2.2</version></dependency><!-- Apache Commons IO --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-io</artifactId><version>1.3.2</version></dependency><!-- JSTL for c: tag --><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency></dependencies>
If you have a simple web application, add following JAR files in WEB-INF/lib folder. You can download all these JARs with source code at the end of this tutorial.
2. Model – Form Object
Create a Java bean which acts as Model/Form object for our Spring application. This bean contains a List of org.springframework.web.multipart.MultipartFile objects. Spring framework provides a useful class MultipartFile which can be used to fetch the file content of uploaded file. Apart from its content, the MultipartFile object also gives you other useful information such as filename, file size etc.
Now create the view pages for this application. We will need two JSPs, one to display file upload form and another to show result on successful upload.
The file_upload_form.jsp displays a form with file input. Apart from this we have added small jquery snippet onclick of Add button. This will add a new file input component at the end of form. This allows user to upload as many files as they want (subjected to file size limit ofcourse).
Note that we have set enctype=”multipart/form-data” attribute of our <form> tag.
Note that we defined the file input name as files, files etc. This will map the submitted files to the List object correctly.
I would suggest you to go through this tutorial to understand how Spring maps multiple entries from form to bean: Multiple Row Form Submit using List of Beans
Second view page is to display filename of uploaded file. It simply loops through filename list and display the names.
In Spring configuration (spring-servlet.xml) we define several important configurations. Note how we defined bean multipartResolver. This will make sure Spring handles the file upload correctly using CommonsMultipartResolver class.
Execute the project in Eclipse. Open following URL in browser to see file upload form.
Select files through file dialog and press Upload button to upload. Following page will displayed with list of files being uploaded.