Spring MVC Multiple File Upload example

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.

FileUploadForm.java

package net.viralpatel.spring3.form;

import java.util.List;

import org.springframework.web.multipart.MultipartFile;

public class FileUploadForm {

	private List<MultipartFile> files;
	
	//Getter and setter methods
}

3. Controller – Spring Controller

Create a Spring 3 MVC based controller which handles file upload. There are two methods in this controller:

  1. displayForm – Is a used to show input form to user. It simply forwards to the page file_upload_form.jsp
  2. save – Fetches the form using @ModelAttribute annotation and get the File content from it. It creates a list of filenames of files being uploaded and pass this list to success page.

FileUploadController.java

package net.viralpatel.spring3.controller;

import java.util.ArrayList;
import java.util.List;

import net.viralpatel.spring3.form.FileUploadForm;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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;

@Controller
public class FileUploadController {
	
	@RequestMapping(value = "/show", method = RequestMethod.GET)
	public String displayForm() {
		return "file_upload_form";
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(
			@ModelAttribute("uploadForm") FileUploadForm uploadForm,
					Model map) {
		
		List<MultipartFile> files = uploadForm.getFiles();

		List<String> fileNames = new ArrayList<String>();
		
		if(null != files && files.size() > 0) {
			for (MultipartFile multipartFile : files) {

				String fileName = multipartFile.getOriginalFilename();
				fileNames.add(fileName);
				//Handle file content - multipartFile.getInputStream()

			}
		}
		
		map.addAttribute("files", fileNames);
		return "file_upload_success";
	}
}

4. View – JSP views

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.

file_upload_form.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
	<title>Spring MVC Multiple File Upload</title>
<script 
src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
	//add more file components if Add is clicked
	$('#addFile').click(function() {
		var fileIndex = $('#fileTable tr').children().length - 1;
		$('#fileTable').append(
				'<tr><td>'+
				'	<input type="file" name="files['+ fileIndex +']" />'+
				'</td></tr>');
	});
	
});
</script>
</head>
<body>
<h1>Spring Multiple File Upload example</h1>

<form:form method="post" action="save.html" 
		modelAttribute="uploadForm" enctype="multipart/form-data">

	<p>Select files to upload. Press Add button to add more file inputs.</p>

	<input id="addFile" type="button" value="Add File" />
	<table id="fileTable">
		<tr>
			<td><input name="files[0]" type="file" /></td>
		</tr>
		<tr>
			<td><input name="files[1]" type="file" /></td>
		</tr>
	</table>
	<br/><input type="submit" value="Upload" />
</form:form>
</body>
</html>

Note that we defined the file input name as files[0], files[1] 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.

file_upload_success.jsp

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
	<title>Spring MVC Multiple File Upload</title>
</head>
<body>
	<h1>Spring Multiple File Upload example</h1>
	<p>Following files are uploaded successfully.</p>
	<ol>
		<c:forEach items="${files}" var="file">
			<li>${file}</li>
		</c:forEach>
	</ol>
</body>
</html>

5. Spring Configuration

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.

spring-servlet.xml

<?xml  version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<context:annotation-config />
	<context:component-scan base-package="net.viralpatel.spring3.controller"/>
 
 <bean id="multipartResolver"
  class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

  <bean id="jspViewResolver"
	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
  </bean>
</beans>

6. Output

Execute the project in Eclipse. Open following URL in browser to see file upload form.

URL: http://localhost:8080/Spring3MVC_Multiple_File_Upload_example/show.html

spring-mvc-multiple-file-upload-demo

Select files through file dialog and press Upload button to upload. Following page will displayed with list of files being uploaded.

spring-mvc-multi-file-upload-success

We have added a small Javascript snippet for Add button. This will add more file upload components to the page. Use this if you want to upload more files.

Download Source Code

SpringMVC_Multi_File_Upload_example.zip (3.6 MB)



15 Comments

  • Yogesh 22 November, 2012, 10:21

    Hi Viral! A great tutorial! How do we restrict the file types being uploaded. Use case being i want the user to upload only .pdf file or .xls files.

    • Greg 25 January, 2013, 18:41

      You can use a validator, first register your validator

      	@InitBinder("uploadItem")
      	void initBinder(WebDataBinder binder) {
      		binder.setValidator(new UploadValidator ());
      	}
      	
      

      Then you apply that validator to uploaded file

      
          public String uploadPluginAction(@Valid @ModelAttribute("uploadItem") FileUploadBean uploadItem, BindingResult result, ModelMap modelMap,
                  HttpServletRequest request, HttpServletResponse response)
          {
              logger.trace("Upload  Handler");
              // Check for validation error
              if (result.hasErrors())
              {
      
                  return "/add";
              }
      }
      
      mport java.io.IOException;
      
      import org.apache.log4j.Logger;
      import org.springframework.validation.Errors;
      import org.springframework.validation.Validator;
      import org.springframework.web.multipart.MultipartFile;
      
      public class  UploadValidator implements Validator {
      	private static transient Logger logger = Logger.getLogger(UploadValidator.class);
      	// Content types the user can upload
      	private static final String[] ACCEPTED_CONTENT_TYPES = new String[] {
      			"application/pdf",
      			"application/doc",			
      			"application/msword",
      			"application/rtf",			
      			"text/richtext" , 
      			"text/rtf" , 
      			"text/plain" , 
      			"application/vnd.openxmlformats-officedocument.wordprocessingml.document" , 
      			"application/vnd.sun.xml.writer" ,
      			"application/x-soffice" ,
      			};
      	
      	private static final String[] ACCEPTED_EXTENSIONS = new String[] {
      		"doc",
      		"pdf",
      		"docx",
      		"rtf",	
      		"txt",	
      	};
      	
      	@Override
      	public boolean supports(Class<?> clazz) {
      		return FileUploadBean.class.equals(clazz);
      	}
      
      	/**
      	 * Validate our uploaded bean
      	 */
      	@Override
      	public void validate(Object obj, Errors errors) {
      		FileUploadBean uploadItem = (FileUploadBean) obj;
      		MultipartFile file = uploadItem.getFile();
      		try {
      			if(file == null || file.getBytes().length == 0){
      				errors.reject("error.upload.null", "File name can't be empty");
      				return;
      			}
      		} catch (IOException e) {
      			logger.error(e.getMessage());			
      		}		
      		
      		// Check content type
      		boolean acceptableContentType = false;
      		String incomingContentType = file.getContentType();
      		logger.info("FileName = "+file.getName());		
      		logger.info("incomingContentType = "+incomingContentType);
      		// This related to bug  when on Vista and using Firefox content type is 'application/octet-stream'		  
      		// Instead  of one of the allowed ones
      		if("application/octet-stream".equalsIgnoreCase(incomingContentType)){
      			int index = file.getOriginalFilename().lastIndexOf('.');
      			String incomingExtension = file.getOriginalFilename().substring(index + 1);
      			for(String extendsion : ACCEPTED_EXTENSIONS){
      				if(extendsion.equalsIgnoreCase(incomingExtension)){
      					acceptableContentType = true;
      					break;
      				}			
      			}
      		}else{
      			for(String contentType : ACCEPTED_CONTENT_TYPES){
      				logger.debug("Comparing " + incomingContentType +" to "+ contentType);
      				if(contentType.equalsIgnoreCase(incomingContentType)){
      					acceptableContentType = true;
      					break;
      				}			
      			}
      		}
      		if(!acceptableContentType){
      			errors.reject("error.upload.contenttype", "Please upload a file with one of the following file types; .doc, .docx, .txt, .pdf, .rtf .");
      		}
      	}
      }
      
      

      I hope this helps

  • Auton 15 December, 2012, 19:28

    Hello there, nice tutorial. Just one warn, when I run your example after download it from here and I get the following exception stacktrace:

    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
    Field error in object 'uploadForm' on field 'files[0]': rejected value [[org.springframework.web.multipart.commons.CommonsMultipartFile@21e4d4, org.springframework.web.multipart.commons.CommonsMultipartFile@1363716]]; codes [typeMismatch.uploadForm.files[0],typeMismatch.uploadForm.files,typeMismatch.files[0],typeMismatch.files,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [uploadForm.files[0],files[0]]; arguments []; default message [files[0]]]; default message [Failed to convert property value of type 'java.util.LinkedList' to required type 'org.springframework.web.multipart.MultipartFile' for property 'files[0]'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.util.LinkedList] to required type [org.springframework.web.multipart.MultipartFile] for property 'files[0]': no matching editors or conversion strategy found]
    

    You shoul change the line of code:

    var fileIndex = $('#fileTable tr').children().length - 1;
    

    for this one:

    var fileIndex = $('#fileTable tr').children().length;
    

    doing so, it wont duplicate the same last index when select the “Add File” button

    Regards!

  • Cagatay 4 January, 2013, 14:51

    Very good tutorial, thanks.

  • argan 5 January, 2013, 11:59

    this is very good to learn java, keep exist with this site such we can to be java expert hahahahaha., thanks viralpatel.,

  • Hariharan 14 January, 2013, 15:22

    Thanks for this example. Its easy to understand, I have downloaded the source code, but i am not able to run the project.

    http://localhost:9090/SpringMVC_Multi_File_Upload_example/

    i am getting 404 error. what is missing?.

  • Aks13 22 January, 2013, 1:44

    Spring Multiple File Upload example
    Following files are uploaded successfully.

    CreateDB.sql

    Where is place uploaded files ?

  • warika 22 January, 2013, 22:09

    I want to using ajax(not submit form), read the selected file in input file. You can help me…

  • rudi 3 February, 2013, 16:08

    hai all
    can you help me now?
    For some of my homework (one of many problems)
    “how to uploading file xls with asp mvc 3 to database? ”
    share with me please, simple sample
    programming ASP MVC3
    Database SQL SERVER
    example databases name_DB = db_school, name_table = tbl_student, Field = – id, name

  • Sid 4 February, 2013, 10:29

    Thanks for detailed explanation. Before visiting to this page I didn’t knew anything about file upload, Now i can write file upload code easily..

    Regards
    sid

  • Sudipto Saha 20 February, 2013, 15:40

    I was not sure where the files were uploaded…
    So,I just got your code a little modified with d help of some other example:
    And here is the full running code:
    1.Just Create D:\Test\Upload directory structure or whatever.
    2.Copy this code to FileUploadController.java

    package net.viralpatel.spring3.controller;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import net.viralpatel.spring3.form.FileUploadForm;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    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;
    
    @Controller
    public class FileUploadController {
    	private String saveDirectory = "D:/Test/Upload/"; //Here I Added
    	
    	@RequestMapping(value = "/show", method = RequestMethod.GET)
    	public String displayForm() {
    		return "file_upload_form";
    	}
    	
    	@RequestMapping(value = "/save", method = RequestMethod.POST)
    	public String save(
    			@ModelAttribute("uploadForm") FileUploadForm uploadForm,
    					Model map) throws IllegalStateException, IOException {
    		
    		List<MultipartFile> files = uploadForm.getFiles();
    
    		List<String> fileNames = new ArrayList<String>();
    		
    		if(null != files && files.size() > 0) {
    			for (MultipartFile multipartFile : files) {
    
    				String fileName = multipartFile.getOriginalFilename();
    				fileNames.add(fileName);
    				//Handle file content - multipartFile.getInputStream()
    				multipartFile.transferTo(new File(saveDirectory + multipartFile.getOriginalFilename()));   //Here I Added
    			}
    		}
    		
    		map.addAttribute("files", fileNames);
    		return "file_upload_success";
    	}
    }
    
    
  • Dhoni 4 March, 2013, 22:18

    cool machi…yenga tale viral-kku oru ooooooo podu…

  • prem 28 March, 2013, 11:52

    Hi viral could you please rectify this following exception

    SEVERE: Context initialization failed
    org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 12 in XML document from ServletContext resource [/WEB-INF/dispatcher-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'context:annotation-config'.
    	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
    	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
    SEVERE: WebModule[/SpringFileUpload]StandardWrapper.Throwable
    org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 12 in XML document from ServletContext resource [/WEB-INF/dispatcher-servlet.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'context:annotation-config'.
    	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
    	at java.lang.Thread.run(Thread.java:619)
    
    SEVERE: Exception while invoking class com.sun.enterprise.web.WebApplication start method
    java.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'context:annotation-config'.
    	at com.sun.enterprise.web.WebApplication.start(WebApplication.java:138)
    	at org.glassfish.internal.data.EngineRef.start(EngineRef.java:130)
    
    SEVERE: Exception while loading the app
    SEVERE: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'context:annotation-config'.
    
  • Vamshi 18 April, 2013, 0:08

    This really helped me. Struggled lot of time to get this done before seeing this tutorial/topic

    Thankyou.

  • sumeet singh 27 April, 2013, 11:28

    very nice tutorial for beginners.

Leave a Reply

Your email address will not be published. Required fields are marked *

Note

To post source code in comment, use [code language] [/code] tag, for example:

  • [code java] Java source code here [/code]
  • [code html] HTML here [/code]