Spring MVC + jQuery File Upload (multiple, drag&drop, progress…)

jQuery-File-Upload js library is “so far” the most elegant, smart, simple..etc js library I have used to upload files. Simply it is amazing. It supports async upload “Ajax”, multiple files upload, drag&drop, progress update and a lot more. Here we will see an example of uploading multiple files to Spring MVC app. Bootstrap has been used in this example to make the UI more attractive.




  • Firefox & Chrome (IE was not tested!)
  • Maven
  • Eclipse
  • Jetty (or other java server)


Java Libraries:

  • Spring Framework
  • Apache common io & fileupload
  • Jackson
  • Common logging
  • Other

check .classpath file for complete list of jars



Javascript Libraries:

<script src="js/jquery.1.9.1.min.js"></script>
<script src="js/vendor/jquery.ui.widget.js"></script>
<script src="js/jquery.iframe-transport.js"></script>
<script src="js/jquery.fileupload.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
<link href="bootstrap/css/bootstrap.css" type="text/css" rel="stylesheet" />

The example was designed to run with simple Maven command, just download the source code and run it.

>mvn jetty:run

( 1 ) Backend – Java Project “Spring MVC”

This project was generate using Maven and converted into Eclipse

For the backend we have:

  • 2 Java files (FileController.java & FileMeta.java)
  • 2 XML files (rest-servlet.xml & web.xml)
  • src/main/java/com/hmkcode/spring/mvc/controllers/FileController.java
package com.hmkcode.spring.mvc.controllers;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.hmkcode.spring.mvc.model.FileMeta;

public class FileController {

	LinkedList<FileMeta> files = new LinkedList<FileMeta>();
	FileMeta fileMeta = null;
	 * URL: /rest/controller/upload  
	 * upload(): receives files
	 * @param request : MultipartHttpServletRequest auto passed
	 * @param response : HttpServletResponse auto passed
	 * @return LinkedList<FileMeta> as json format
	@RequestMapping(value="/upload", method = RequestMethod.POST)
	public @ResponseBody LinkedList<FileMeta> upload(MultipartHttpServletRequest request, HttpServletResponse response) {

		//1. build an iterator
		 Iterator<String> itr =  request.getFileNames();
		 MultipartFile mpf = null;

		 //2. get each file

			 //2.1 get next MultipartFile
			 mpf = request.getFile(itr.next()); 
			 System.out.println(mpf.getOriginalFilename() +" uploaded! "+files.size());

			 //2.2 if files > 10 remove the first from the list
			 if(files.size() >= 10)

			 //2.3 create new fileMeta
			 fileMeta = new FileMeta();
			 fileMeta.setFileSize(mpf.getSize()/1024+" Kb");

			 try {

                 // copy file to local disk (make sure the path "e.g. D:/temp/files" exists)			
                 FileCopyUtils.copy(mpf.getBytes(), new FileOutputStream("D:/temp/files/"+mpf.getOriginalFilename()));

			} catch (IOException e) {
				// TODO Auto-generated catch block
			 //2.4 add to files
		// result will be like this
		// [{"fileName":"app_engine-85x77.png","fileSize":"8 Kb","fileType":"image/png"},...]
		return files;
	 * URL: /rest/controller/get/{value}
	 * get(): get file as an attachment
	 * @param response : passed by the server
	 * @param value : value from the URL
	 * @return void
	@RequestMapping(value = "/get/{value}", method = RequestMethod.GET)
	 public void get(HttpServletResponse response,@PathVariable String value){
		 FileMeta getFile = files.get(Integer.parseInt(value));
		 try {		
			 	response.setHeader("Content-disposition", "attachment; filename=\""+getFile.getFileName()+"\"");
		        FileCopyUtils.copy(getFile.getBytes(), response.getOutputStream());
		 }catch (IOException e) {
				// TODO Auto-generated catch block


package com.hmkcode.spring.mvc.model;

import org.codehaus.jackson.annotate.JsonIgnoreProperties;

//ignore "bytes" when return json format
public class FileMeta {

	private String fileName;
	private String fileSize;
	private String fileType;

	private byte[] bytes;

         //setters & getters


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
id="WebApp_ID" version="2.5">
  <display-name>Archetype Created Web Application</display-name>



<beans xmlns="http://www.springframework.org/schema/beans"







	<context:component-scan base-package="com.hmkcode.spring.mvc.controllers" />
 	<mvc:annotation-driven />
 	<bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

( 2 ) Frontend (HTML, CSS, JS)

  • 1 HTML file (index.html)
  • 1 JS file (myuploadfunction.js)
  • 1 CSS file (dropzone.css)

Other .js, .css, images files are downloaded

  • /scr/main/webapp/index.html
<meta charset="utf-8">
<title>jQuery File Upload Example</title>
<script src="js/jquery.1.9.1.min.js"></script>

<script src="js/vendor/jquery.ui.widget.js"></script>
<script src="js/jquery.iframe-transport.js"></script>
<script src="js/jquery.fileupload.js"></script>

<!-- bootstrap just to have good looking page -->
<script src="bootstrap/js/bootstrap.min.js"></script>
<link href="bootstrap/css/bootstrap.css" type="text/css" rel="stylesheet" />

<!-- we code these -->
<link href="css/dropzone.css" type="text/css" rel="stylesheet" />
<script src="js/myuploadfunction.js"></script>

<h1>Spring MVC - jQuery File Upload</h1>
<div style="width:500px;padding:20px">

    <input id="fileupload" type="file" name="files[]" data-url="rest/controller/upload" multiple>

    <div id="dropzone">Drop files here</div>

    <div id="progress">
        <div style="width: 0%;"></div>

    <table id="uploaded-files">
            <th>File Name</th>
            <th>File Size</th>
            <th>File Type</th>

  • /scr/main/webapp/js/myuploadfunction.js
$(function () {
        dataType: 'json',

        done: function (e, data) {
            $.each(data.result, function (index, file) {

                		.append($('<td/>').html("<a href='rest/controller/get/"+index+"'>Click</a>"))
                		)//end $("#uploaded-files").append()

        progressall: function (e, data) {
	        var progress = parseInt(data.loaded / data.total * 100, 10);
	        $('#progress .bar').css(
	            progress + '%'

		dropZone: $('#dropzone')
  • /scr/main/webapp/css/dropzone.css
#dropzone {
    background: #ccccc;
    width: 150px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    font-weight: bold;
#dropzone.in {
    width: 600px;
    height: 200px;
    line-height: 200px;
    font-size: larger;
#dropzone.hover {
    background: lawngreen;
#dropzone.fade {
    -webkit-transition: all 0.3s ease-out;
    -moz-transition: all 0.3s ease-out;
    -ms-transition: all 0.3s ease-out;
    -o-transition: all 0.3s ease-out;
    transition: all 0.3s ease-out;
    opacity: 1;

( 3 ) Run the Example

./spring-mvc-jquery-file-upload>mvn jetty:run


Source Code @ github

Related posts from Spring Framework Tutorial

11 thoughts on “Spring MVC + jQuery File Upload (multiple, drag&drop, progress…)

  1. Chintan Patel


    I am working on java – spring mvc. I am submitting my data through JSON.

    What I want to do is:
    I want to upload an image instantly in to temporary folder on my server using , and show what is to be uploaded.

    On final submit all data including uploaded image path (temporary folder’ path) will be passed to my spring controller through JSON object, then transfer image from temporary directory to actual directory & saved all data in to a database.

    There are few questions:
    1. Where the file is to be uploaded (location)?
    2. Where to give folder path?

    I would be appreciated if you can hep me.

    Thanks in advance.

  2. Sridhar

    Above code segment is working great. But the problem is in IE once uploaded, its giving as downloadable file with unknown format(Some issue with IE in uploading).
    Can you please confirm these scripts will work IE also?

  3. Majid

    Nice tutorial, Can you please tell me where are the uploaded files located in my PC ?

    1. hani.hmk Post author

      For this example, we don’t save uploaded files, we keep them on the memory.

        1. hani.hmk Post author

          I updated the “FileController.java” to copy files on local disk

          // copy file to local disk (make sure the path “e.g. D:/temp/files” exists)
          FileCopyUtils.copy(mpf.getBytes(), new FileOutputStream(“D:/temp/files/”+mpf.getOriginalFilename()));

  4. Dante


    That’s awesome, It works like a charm but I need to separate the content server from the server which hosts my web application. So I tried to test this in two separate instance of tomcat (that should be two different domains in the real app) and I changed the “data-url” to ” http://localhost:8084/rest/controller/upload” instead of “rest/controller/upload” but I faced with this error:

    “XMLHttpRequest cannot load http://localhost:8084/rest/controller/upload. Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin.”

    I appreciate your comment ;)

  5. Latifaah

    I downloaded this project a while ago, it was working, I just downloaded it today , tried it in eclipse but I got this error :

    Description Resource Path Location Type
    Project ‘spring-mvc-jquery-file-upload’ is missing required source folder: ‘src/main/resources’ spring-mvc-jquery-file-upload Build path Build Path Problem

    Please your help is appreciated.

Comments are closed.