Controller Layer

Now we will create back-end logic for handling multiple files uploading process after that save those files into one folder or specific location. This code is developed under controller layer.

FileUploadController.java:

Java




package com.gfg.articles;
  
import java.io.File;
import java.io.IOException;
  
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
  
@Controller
public class FileUploadController {
  
    private static final String UPLOAD_DIR = "./src/upload/";
  
    @RequestMapping("/")
    public String index() {
        return "index";
    }
      
    @PostMapping("/upload")
    @ResponseBody
    public ResponseEntity<String> handleFileUpload(@RequestParam("files") MultipartFile[] files) {
        if (files == null || files.length == 0) {
            return ResponseEntity.badRequest().body("No files selected for upload.");
        }
  
        try {
            for (MultipartFile file : files) {
                // Get the file name
                String fileName = file.getOriginalFilename();
  
                // Create the directory if it doesn't exist
                File directory = new File(UPLOAD_DIR);
                if (!directory.exists()) {
                    directory.mkdirs();
                }
  
                // Save the file to the server
                File serverFile = new File(directory.getAbsolutePath() + File.separator + fileName);
                file.transferTo(serverFile);
            }
  
            return ResponseEntity.ok("Files uploaded successfully!");
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body("Error uploading files: " + e.getMessage());
        }
    }
}


In the above code we have created different types of end points for different operations like display the html file, uploading files and others. Now we will discuss each end point.

  • @RequestMapping(“/”) this end point is used for displaying the HTML page by using Thymeleaf.
  • @PostMapping(“/upload”) this end point is used for uploading the files into given location.
  • In the above code I take One location for saving uploaded files.

Here MultiPartFile is used for dealing with files from front-end to back-end if file is null or file length equals to 0 then we will throw one error that is no files selected for upload. And this message triggered on HTML page through Thymeleaf. Otherwise, files are successfully uploaded into given location.

Spring MVC – Multiple File Upload with Progress Bar in Ajax and JQuery

File Uploading process plays an important role in data collection, Information sharing, and other situations. The File Uploading process is done in two instances. Those are the single file upload process and the other one uploading multiple files at a time. It depends upon the logic of the application. In this article, we will discuss the Multiple File Uploading process by using Spring MVC with the help of Ajax and JQuery to display the progress bar while uploading the files into one folder. If all files are uploaded the progress bar shows 100% completion on the HTML page. ( Spring MVC Reference )

Prerequisites

  • Spring Boot Framework with MVC Pattern
  • Thymeleaf
  • Ajax
  • JQuery
  • Bootstrap
  • One Folder for saving uploaded files.
  • Project Category is Gradle

The Spring Boot with MVC Pattern is used for triggering the Front-End Logic. For this, we have used Thymeleaf, which works as a bridge between Front-End and Back-End. Any update in the Back end then the Thymeleaf can Trigger the HTML content based upon the back-end functionality. Ajax and JQuery are used for providing Dynamic Behavior of the HTML page and Bootstrap is used for Creating Web Page with Responsive Behavior. After that create one folder in your system for saving the uploaded files.

Similar Reads

Steps by Step Implementation for Multiple File Upload

Create a Spring Stater Project using Spring Tool Suite. (Create a Spring Boot Project) While Creating the Project Select the required Dependencies (mentioned below) for this Application. After completion of the project creation In the Main Project Package create one Controller class for Handling the Multiple File Upload process After That create one HTML File in the Templates which is located in the Resources folder in the main project file and develop the Front-End code using HTML, Bootstrap is used for creating the Template, and Ajax and JQuery are used for providing the Dynamic Behavior for Progress Bar. Once completed both Front-End and Back-End Development Then run this project as Run a Spring Boot App. Then open localhost with the default port number in Browser....

Controller Layer

Now we will create back-end logic for handling multiple files uploading process after that save those files into one folder or specific location. This code is developed under controller layer....

View Layer

...

Conclusion

Normally HTML, JSP and other files comes under view layer. In this Application I used HTML pages for displaying user interface. Now we will see the Front-End Code with Its functionality....

Contact Us