Spring Boot File Upload with Advance Progress bar in Ajax

The multiple file upload with the progress bar in the web application gives the best user experience to the end-user. This article shows you how to upload files in the Spring Boot web application with an advanced progress bar to achieve the best user experience.

Let’s have a look at the image below, this is what you can build at the end of this article.

Spring Boot File Upload with Advance Progress bar in Ajax

Software Dependency

We hope the above software dependencies are already available in your system if not follow the specified installation steps to set up your spring boot application environment.

Create Spring Boot File Upload Application

Let’s open Eclipse IDE, click on File > New > Spring Starter Project then provide the below details as you can see in the image.

Name: SpringBootFileUpload, Group: com.javacodepoint, Artifact: SpringBootFileUpload, Package: com.javacodepoint.fileupload

Spring Boot File Upload ajax

Select the required Spring Starter Dependency

  • spring-boot-starter-web (Spring Web)
  • spring-boot-starter-thymeleaf (Thymeleaf)
  • spring-boot-devtools (Spring Boot DevTools)
Spring boot file upload | spring starter dependencies

Spring Boot File Upload Ajax Project Structure

Spring boot Maven eclipse project structure will look like the below image:

spring boot eclipse project structure


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
		<relativePath /> <!-- lookup parent from repository -->
	<description>Spring Boot File Upload</description>

Understand Required Spring Boot Dependencies

spring-boot-starter-web (Spring Web) :

It is used for building web applications, including RESTful applications using Spring MVC. It uses Tomcat as the default embedded container. The single spring-boot-starter-web dependency pulls in all dependencies related to web development.

spring-boot-starter-thymeleaf (Thymeleaf) :

Thymeleaf is a Java library. It is an XML/XHTML/HTML5 template engine that can apply a set of transformations to template files to display data and/or text produced by your applications.

spring-boot-devtools (Spring Boot DevTools) :

DevTools includes an embedded LiveReload server. LiveReload is a simple protocol that allows your application to automatically trigger a browser refresh whenever things change.

spring-boot-starter-test (Spring Boot Test) :

This starter includes Spring-specific dependencies and dependencies for auto-configuration and a set of testing libraries such as JUnit, etc.

Javascript Ajax Progress Bar Implementation

Let’s understand the implementation of the progress bar with sample points by referring to the below filesupload.html file:

  1. In the below example, we are taking some global variables(totalFileCount, fileUploadCount, fileSize, successCount) to store specific data.
  2. When the user will click on the Upload file button, the function startUploading() will be called. This is the initiation function of file upload. Here we are initializing the defined global variables.
  3. Before initiating the file uploads, we are calling the function prepareProgressBarUI() to design the progress bar UI.
  4. Here we are using XMLHttpRequest a Javascript class object to do the Ajax call. You can see in the function uploadFile().
  5. FormData a class object is used to attach the multipart file using append() method in order to upload it.
  6. progress, load, and error are the events that are fired when file upload happens. So we are going to bind these events with function onUploadProgress(), onUploadComplete(), and onUploadError() respectively.
  7. The function onUploadProgress() will be continuously called and updated on the progress bar. Here e.loaded will tell how many bytes have been uploaded to the server, so based on that we are calculating the percentage of the progress bar.
  8. The function onUploadComplete() will be called when a file gets uploaded completely to the server. Here we are doing the next Ajax call if more files are yet to be uploaded otherwise updating 100% for the progress bar.
  9. The function onUploadError() will be called when an error occurs while uploading the files.


<link rel="stylesheet"
<link rel="stylesheet"
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
.table>tbody>tr>td{padding: 2px 6px;vertical-align: middle;border:none;}
#main-container{padding: 0px 20px 40px; width: 52%;}
#upload-header{height:35px;width:100%;background-color: #323254;color: #fff;padding: 8px;border-top-left-radius: 10px;border-top-right-radius: 10px;}
#progress-bar-container{padding:20px;max-height:260px;overflow-y:auto;border:1px solid #323254;}
::-webkit-scrollbar {background-color: #fff; width: 8px; height: 8px;}
::-webkit-scrollbar-thumb {background-color: #C0C0C0; border-radius: 10px;}
    <div id="main-container">
        <h3 class="text-info">Spring Boot file upload with advance progress-bar using Ajax</h3>
        <div style="margin-bottom: 20px">
            <input type="file" id="files" multiple style="margin-bottom: 20px" />
            <button class="btn btn-primary" type="button" onclick="startUploading()" ><i class="fa fa-upload"></i> Upload file</button>
        <div id="upload-status-container">
            <div id="upload-header">
                <span id="upload-header-text"></span>
                <i class="action-icon fa fa-window-minimize pull-right" onclick="showHide(this)" title="minimize"></i>
            <div id="progress-bar-container">
                <table class="table">
    /* Globle variables */
    var totalFileCount, fileUploadCount, fileSize, successCount;
    /* start uploading files */
    function startUploading() {
        var files = document.getElementById('files').files;
            alert("Please choose at least one file and try.");
        successCount = 0;
        // upload through ajax call     
    /* This method will be called to prepare progress-bar UI */
    function prepareProgressBarUI(files){
        totalFileCount = files.length;
        var $tbody=$("#progress-bar-container").find("tbody");
        $("#upload-header-text").html("1 of "+totalFileCount+" file(s) is uploading");
        for(var i=0;i<totalFileCount;i++){
            var fsize=parseFileSize(files[i].size);
            var fname=files[i].name;
            var bar='<tr id="progress-bar-'+i+'"><td style="width:75%"><div class="filename">'+fname+'</div>'
            +'<div class="progress"><div class="progress-bar progress-bar-striped active" style="width:0%"></div></div><div class="error-msg text-danger"></div></td>'
            +'<td  style="width:25%"><span class="size-loaded"></span> '+fsize+' <span class="percent-loaded"></span></td></tr>';
    /* parse the file size in kb/mb/gb */
    function parseFileSize(size){
        var precision=1;
        var factor = Math.pow(10, precision);
        size = Math.round(size / 1024); //size in KB
        if(size < 1000){
            return size+" KB";
            size = Number.parseFloat(size / 1024); //size in MB
            if(size < 1000){
                return (Math.round(size * factor) / factor) + " MB";
                size = Number.parseFloat(size / 1024); //size in GB
                return (Math.round(size * factor) / factor) + " GB";
        return 0;
    /* one by one file will be uploaded to the server by ajax call*/
    function uploadFile() {
        var file = document.getElementById('files').files[fileUploadCount];
        fileSize = file.size;
        var xhr = new XMLHttpRequest();
        var fd = new FormData();
        fd.append("multipartFile", file);
        xhr.upload.addEventListener("progress", onUploadProgress, false);
        xhr.addEventListener("load", onUploadComplete, false);
        xhr.addEventListener("error", onUploadError, false);
        xhr.open("POST", "/api/fileupload");
    /* This function will continueously update the progress bar */
    function onUploadProgress(e) {
        if (e.lengthComputable) {
        	var loaded = e.loaded;
            var percentComplete = parseInt((loaded) * 100 / fileSize);
            if(percentComplete < 100){
            	var pbar = $('#progress-bar-'+fileUploadCount);
                var bar=pbar.find(".progress-bar");
                var sLoaded=pbar.find(".size-loaded");
                var pLoaded=pbar.find(".percent-loaded");
            	bar.css("width",percentComplete + '%');
                sLoaded.html(parseFileSize(loaded)+ " / ");
                pLoaded.html("("+percentComplete+ "%)");
        } else {
            alert('unable to compute');
    /* This function will call when upload is completed */
    function onUploadComplete(e, error) {
        var pbar = $('#progress-bar-'+fileUploadCount);
    	var bar = pbar.find(".progress-bar");
        if(e.currentTarget.responseText!="" || error){
        	pbar.find(".error-msg").html(e.currentTarget.responseText || "Something went wrong!");
        	var sLoaded=pbar.find(".size-loaded");
        	var pLoaded=pbar.find(".percent-loaded");
        	sLoaded.html('<i class="fa fa-check text-success"></i> ');
        if (fileUploadCount < totalFileCount) {
            //ajax call if more files are there 
            $("#upload-header-text").html((fileUploadCount+1)+" of "+totalFileCount+" file(s) is uploading");
        } else if(successCount==0){
            $("#upload-header-text").html("Error while uploading");
        } else{
        	$("#upload-header-text").html(successCount+" of "+totalFileCount+" file(s) uploaded successfully");
    /* This function will call when an error come while uploading */
    function onUploadError(e) {
        console.error("Something went wrong!");
    function showHide(ele){

Create File Upload Rest Controller

Let’s understand the file upload rest controller with sample points by referring the below FileUploadRestController.java file:

  1. Spring @RestController annotation is used to create RESTful web services using Spring MVC. Spring RestController takes care of mapping request data to the defined request handler method.
  2. @PostMapping annotated methods handle the HTTP POST requests matched with the given URI expression (eg. @PostMapping(“/api/fileupload”) ).
  3. The @RequestParam is used to read the HTML form data provided by a user and bind it to the request parameter. Here we are using it to collect MultipartFile, Eg- @RequestParam(“multipartFile”) MultipartFile uploadfile.
  4. UPLOAD_PATH = “C://uploads//”, we are taking it for a location to save uploaded files on the server.
  5. Files.write(), the method is used to save the collected files on a specified server location.


package com.javacodepoint.fileupload;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

public class FileUploadRestController {

     * Location to save uploaded files on server
	private static String UPLOAD_PATH = "C://uploads//";

	 * single file upload in a request
	public void uploadFile(@RequestParam("multipartFile") MultipartFile uploadfile) {

		if (uploadfile.isEmpty()) {
			System.out.println("please select a file!");

		try {
			byte[] bytes = uploadfile.getBytes();
			Path path = Paths.get(UPLOAD_PATH + uploadfile.getOriginalFilename());
			Files.write(path, bytes);
		} catch (Exception e) {

Multipart Properties Configuration


# Multipart Properties

# Max size for a single file

# Max size for a single request

Create a Welcome Page Controller


package com.javacodepoint.fileupload;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

public class WelcomePageController {

    public String index() {
        return "filesupload";

Create a Global Exception Handler


package com.javacodepoint.fileupload;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartException;

public class GlobleExceptionHandler {
    ResponseEntity<?> handleControllerException(HttpServletRequest request, Throwable e) {
        HttpStatus status = getStatus(request);
        return new ResponseEntity<String>(e.getMessage(), status);

    private HttpStatus getStatus(HttpServletRequest request) {
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        if (statusCode == null) {
            return HttpStatus.INTERNAL_SERVER_ERROR;
        return HttpStatus.valueOf(statusCode);

Spring Boot File Upload Application Startup

We use the @SpringBootApplication annotation in our Application or Main class to enable a host of features, e.g. Java-based Spring configuration, component scanning, and in particular for enabling Spring Boot’s auto-configuration feature.


package com.javacodepoint.fileupload;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringBootFileUploadApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootFileUploadApplication.class, args);

Run The Spring Boot File Upload Application

Right-click on SpringBootFileUploadApplication.java > Run As > Java Application then it will start the configured Tomcat server as you can see in the below image:

Spring boot file upload | running the application

Let’s access the URL: localhost:8080 in any browser like below:

Spring boot file upload | spring boot multiple file upload in java ajax


In this article, you have seen how to create a Spring boot file upload application with an advanced progress bar using the Spring Tool Eclipse plugin and Maven.

Create a drag-and-drop file uploader UI to upload single as well as multiple files at a time. Visit another article: Drag and Drop File Uploader UI.

Hope you like it!

Related Articles:

Share with friends

Leave a Comment

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