Spring Boot Multiple File Upload Example (2024)
In this tutorial, we'll show you how to upload multiple files in Spring Boot application with following rest endpoints.
POST
method to Upload multiple files usingMultipartFile[] files
as a parameter.GET
method to list all files from the file storage folder.
Create Spring Boot Project from Spring Initializer site https://start.spring.io/
Project Structure
Maven Dependency
All we need is spring-boot-starter-web
dependency in pom.xml, add org.projectlombok
for auto generating getters/setters/constructor.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.techgeeknext</groupId>
<artifactId>spring-boot-upload-multiple-files-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-upload-multiple-files-example</name>
<description>Spring Boot upload multiple files example</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Constant File
Create File Utility class, for keeping constant variables:
package com.techgeeknext.util;
import java.nio.file.Path;
import java.nio.file.Paths;
public final class FileUtil {
private FileUtil() {
// restrict instantiation
}
public static final String folderPath = "incoming-files//";
public static final Path filePath = Paths.get(folderPath);
}
RestController to upload multiple files
Create RestController class and define below rest endpoint:
POST
method to Upload multiple files usingMultipartFile[] files
as a parameter.GET
method to list all files from the file storage folder.
package com.techgeeknext.controller;
import com.techgeeknext.util.FileUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
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;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Controller
//@CrossOrigin(origins = "http://localhost:8082") open for specific port
@CrossOrigin() // open for all ports
public class MultipleFilesUploadController {
/**
* Method to upload multiple files
* @param files
* @return FileResponse
*/
@PostMapping("/upload")
public ResponseEntity<FileUploadResponse> uploadFiles(@RequestParam("files") MultipartFile[] files) {
try {
createDirIfNotExist();
List<String> fileNames = new ArrayList<>();
// read and write the file to the local folder
Arrays.asList(files).stream().forEach(file -> {
byte[] bytes = new byte[0];
try {
bytes = file.getBytes();
Files.write(Paths.get(FileUtil.folderPath + file.getOriginalFilename()), bytes);
fileNames.add(file.getOriginalFilename());
} catch (IOException e) {
}
});
return ResponseEntity.status(HttpStatus.OK)
.body(new FileUploadResponse("Files uploaded successfully: " + fileNames));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED)
.body(new FileUploadResponse("Exception to upload files!"));
}
}
/**
* Create directory to save files, if not exist
*/
private void createDirIfNotExist() {
//create directory to save the files
File directory = new File(FileUtil.folderPath);
if (! directory.exists()){
directory.mkdir();
}
}
/**
* Method to get the list of files from the file storage folder.
* @return file
*/
@GetMapping("/files")
public ResponseEntity<String[]> getListFiles() {
return ResponseEntity.status(HttpStatus.OK)
.body( new File(FileUtil.folderPath).list());
}
}
Start the Main Application
Start the SpringBootUploadMultipleFilesExampleApplication.
package com.techgeeknext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootUploadMultipleFilesExampleApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootUploadMultipleFilesExampleApplication.class, args);
}
}
Test Upload Multiple Files Example
POST - Upload multiple files
Open POSTMAN and select the value to upload the multiple files by using http://localhost:8282/upload?files request.GET - List of uploaded files
Open POSTMAN and get the list of uploaded file names by using http://localhost:8282/files request.
Download Source Code
The full source code for this article can be found on below.
Download it here -
Spring
Boot Upload Multiple Files Example