In my previous tutorial I have shown  Spring MVC Single File Upload Example. This tutorial shows an example on how to upload multiple files using Spring MVC 4.x.
For this tutorial we will create maven based web project in Eclipse.

If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.

The following configurations are required in order to run the application
Eclipse Kepler
JDK 1.8
Tomcat 8
Have maven 3 installed and configured
Spring 4 and Apache commons IO dependencies in pom.xml
Now we will see the below steps how to create a maven based project in Eclipse
Step 1. Create a maven based web project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.

Now enter the required fields (Group Id, Artifact Id) as shown below
Group Id : com.roytuts
Artifact Id : spring-mvc-file
Step 2. Modify the pom.xml file as shown below.

<project xmlns="" xmlns:xsi=""
	<name>spring-mvc-file Maven Webapp</name>
		<!-- Spring -->
		<!-- jstl -->
		<!-- mysql java connector -->
                <!-- file upload -->

Step 3. If you see JRE System Library[J2SE-1.5] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.5], click on Edit button and select the appropriate jdk 1.8 from the next window. Click on Finish then Ok.

Change also the Compiler compliance level as 1.8 from Java -> Compiler.
Step 4. Now when the build process finished then modify the web.xml file with below source code

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns=""
	<!-- dispatcher servlet acts as a front controller for each request/response -->
		<!-- load Spring controllers while dispatcher servlet loads -->
	<!-- map URL suffix as .html -->

Step 5. Create spring-config.xml file under src/main/resources directory with the below source code

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
	xmlns:xsi="" xmlns:p=""
	xmlns:aop="" xmlns:context=""
	xmlns:jee="" xmlns:tx=""
	<!-- Support annotation -->
	<mvc:annotation-driven />

Step 6. Create controllers.xml file under src/main/resources directory

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
	xmlns:xsi="" xmlns:p=""
	<import resource="classpath:spring-config.xml" />
	<!-- Scan the package where Spring Controllers are placed -->
	<context:component-scan base-package="com.roytuts.spring.controllers" />
        <!-- define multi-part bean to define handle file upload -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
	<!-- Resolves logical String-based view names to actual View types -->
	<bean id="viewResolver"
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<!-- Where pages are kept -->
		<property name="prefix" value="/pages/" />
		<!-- What is the page extension -->
		<property name="suffix" value=".jsp" />

Step 7. Create a directory called pages under webapp directory for putting created jsp views.

Step 8. Create Spring controller class which will handle user request and response

package com.roytuts.spring.controllers;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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;
public class FileUploadController {
	public static final String ROOT = "C:/upload-dir/";
	@RequestMapping(value = "uploads")
	public String uploadFiles() {
		return "uploadFiles";
	@RequestMapping(method = RequestMethod.POST, value = "upload/files")
	public String handleFilesUpload(@RequestParam("file") MultipartFile files[], Model map) {
		StringBuilder sb = new StringBuilder();
		for (MultipartFile file : files) {
			if (!file.isEmpty()) {
				try {
					if (!Files.exists(Paths.get(ROOT))) {
						try {
						} catch (IOException ioe) {
					Files.copy(file.getInputStream(), Paths.get(ROOT, file.getOriginalFilename()));
					sb.append("You successfully uploaded " + file.getOriginalFilename() + "!n");
					map.addAttribute("msg", sb);
				} catch (IOException | RuntimeException e) {
					sb.append("Failued to upload " + file.getOriginalFilename() + " => " + e.getMessage()
							+ String.valueOf(e) + "n");
					map.addAttribute("msg", sb);
			} else {
				sb.append("Failed to upload " + file.getOriginalFilename() + " because it was emptyn");
				map.addAttribute("msg", sb);
		return "uploadFiles";

Step 9. Now create uploadFiles.jsp file under webapp/pages directory for handling single file upload

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<%@ taglib uri="" prefix="c"%>
<title>Spring MVC Multiple File Upload Example</title>
	<h2>Spring MVC Multiple File Upload Example</h2>
	<c:if test="${not empty msg}">
	<form method="POST" name="upload_files" enctype="multipart/form-data"
		Select File: <input type="file" name="file" multiple="multiple" /> <br />
		<br /> <input value="Upload" type="submit" />

Step 10. Run the application on Tomcat 8 server, you will see below outputs in the browser

When you hit the URL http://localhost:8080/spring-mvc-file/uploads in the browser

spring multiple files upload

When you browse and select files

spring multiple files upload

When the file successfully uploaded

spring multiple files upload

Check the C:upload-dir

spring multiple files upload

Thanks for reading.


1 thought on “Spring MVC Multiple Files Upload Example

Leave a Reply

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