Spring MVC 4 - JQuery Ajax file upload example with progress bar

Posted on June 10, 2017

In this section, I will show you how to use the JQuery’s Ajax function to upload a file with progress bar in Spring MVC application.

We will use the Bootstrap’s progress bar component and XmlHttpRequest’s onprogress event handler for displaying the file upload progress.

Tools and technologies used for this example are –

  • Spring MVC 4.3.7.RELEASE
  • Bootstrap 3.3.7
  • JQuery 3.2.1
  • Eclipse Neon.3
  • Maven 3.3.9
  • JavaSE 1.8
  • Apache Tomcat  7.0.47 (Embedded)

Let us proceed to write a simple Spring MVC application to upload a file using the JQuery’s Ajax function.

Project structure

Review the following web project structure build using Maven.


Related - How to create a web project using maven build tool in eclipse IDE.

Jar dependencies

Open pom.xml file of your maven project and add the following dependencies in it.

  <!-- Spring MVC Dependency -->

  <!-- Commons Fileupload -->
  <!-- JSTL Dependency -->

  <!-- Servlet Dependency -->

  <!-- JSP Dependency -->

Controller class

Create a controller class to handle the file upload request as follows.


package com.boraji.tutorial.spring.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

 * @author imssbora
public class FileUploadController {

   public String fileUploadForm(Model model) {
      return "fileUploadForm";

   // Handling file upload request
   public ResponseEntity<Object> fileUpload(@RequestParam("file") MultipartFile file)
         throws IOException {

      // Save file on system
      if (!file.getOriginalFilename().isEmpty()) {
         BufferedOutputStream outputStream = new BufferedOutputStream(
               new FileOutputStream(
                     new File("D:/Upload", file.getOriginalFilename())));
         return new ResponseEntity<>("Invalid file.",HttpStatus.BAD_REQUEST);
      return new ResponseEntity<>("File Uploaded Successfully.",HttpStatus.OK);

ResponseEntity represent the HTTP response entity that contains body, header and status code. It can be used in RestTemplate and @Controller method.


JSP View

Create a fileUploadForm.jsp file under src\main\webapp\WEB-INF\views folder to send a file to controller’s handler method via JQuery.ajax() function.


<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html >
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<!-- CSS for Bootstrap -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<!-- JQuery -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<script type="text/javascript">
$(function() {
  $('button[type=submit]').click(function(e) {
    //Disable submit button
    var form = document.forms[0];
    var formData = new FormData(form);
    // Ajax call for file uploaling
    var ajaxReq = $.ajax({
      url : 'fileUpload',
      type : 'POST',
      data : formData,
      cache : false,
      contentType : false,
      processData : false,
      xhr: function(){
        //Get XmlHttpRequest object
         var xhr = $.ajaxSettings.xhr() ;
        //Set onprogress event handler 
         xhr.upload.onprogress = function(event){
          	var perc = Math.round((event.loaded / event.total) * 100);
          	$('#progressBar').text(perc + '%');
          	$('#progressBar').css('width',perc + '%');
         return xhr ;
    	beforeSend: function( xhr ) {
    		//Reset alert message and progress bar
    // Called on success of file upload
    ajaxReq.done(function(msg) {
    // Called on failure of file upload
    ajaxReq.fail(function(jqXHR) {
      		' - '+jqXHR.statusText+')');

  <div class="container">
    <h2>Spring MVC - File Upload Example With Progress Bar</h2>
    <!-- File Upload From -->
    <form action="fileUpload" method="post" enctype="multipart/form-data">
      <div class="form-group">
        <label>Select File</label> 
        <input class="form-control" type="file" name="file">
      <div class="form-group">
        <button class="btn btn-primary" type="submit">Upload</button>
    <br />

    <!-- Bootstrap Progress bar -->
    <div class="progress">
      <div id="progressBar" class="progress-bar progress-bar-success" role="progressbar"
        aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%">0%</div>

    <!-- Alert -->
    <div id="alertMsg" style="color: red;font-size: 18px;"></div>

Spring configuration

Create a web @Configuration class annotated with @EnableWebMvc and @ComponentScan as follows. 

To support multipart handling in Spring application, declare CommonsMultipartResolver bean method in configuration class.


package com.boraji.tutorial.spring.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

 * @author imssbora

@ComponentScan(basePackages = { "com.boraji.tutorial.spring.controller" })
public class WebConfig extends WebMvcConfigurerAdapter {

   public InternalResourceViewResolver resolver() {
      InternalResourceViewResolver resolver = new InternalResourceViewResolver();
      return resolver;

   public MultipartResolver multipartResolver() {
      CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
      multipartResolver.setMaxUploadSize(10485760); // 10MB
      multipartResolver.setMaxUploadSizePerFile(1048576); // 1MB
      return multipartResolver;


Servlet container initialization

Create a container initializer class by extending the AbstractAnnotationConfigDispatcherServletInitializer class as follows.


package com.boraji.tutorial.spring.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

 * @author imssbora
public class MyWebAppInitializer
      extends AbstractAnnotationConfigDispatcherServletInitializer {

   protected Class<?>[] getRootConfigClasses() {
      return new Class[] {};

   protected Class<?>[] getServletConfigClasses() {
      return new Class[] { WebConfig.class };

   protected String[] getServletMappings() {
      return new String[] { "/" };

The AbstractAnnotationConfigDispatcherServletIn.itializer class, implements the WebApplicationInitializer, is implemented in Servlet 3.0+ environments in order to configure the ServletContext programmatically.


Build + Deploy + Run application

Use the following maven commands to build, deploy and run embedded Tomcat server.

mvn clean install  (This command triggers war packaging)

mvn tomcat7:run (This command run embedded tomcat and deploy war file automatically)

You can refer this link to learn how to run the above commands in Eclipse IDE.


Type the following URLs in browser's address bar to open the file upload from.



Select a file and click on Upload button. On successful upload of a file, you will see the below screen. 


Download Sources