In this article, we will show how to upload a file with Spring Boot to a folder located in the filesystem. We will use Spring MultipartFile interface to handle multi-part requests to our Rest API.
2. Application Rest uploading API
Spring Boot application server will provide API with the following endpoints:
URL
Method
Action
/files
GET
Get list of uploaded files
/files
POST
Upload a single file
/files
DELETE
Delete all uploaded files
/files/{filename}
GET
Download specific file
Files will be uploaded into the specific static folder which will be configured in the application.properties.
3. Project structure
The following presents a Maven project structure of the upload file application:
In this structure the following elements can be distinguished:
pom.xml is a Maven configuration file with all necessary dependencies,
Application - the main Spring Boot class that starts the application server,
FilesController - class that handle HTTP requests,
FileService - service class responsible for saving uploaded file in the filesystem and retrieving uploaded files,
RestExceptionHandler - handles MaxUploadSizeExceededException when processing file (could be extended with other exceptions as well),
FileData - contains information about the uploaded file like name, size, location,
application.properties - Spring Boot application properties with a path for uploaded files.
4. Setup Spring Boot application
To create a Spring Boot project from scratch we could use Initializer or other development tools available in Eclipse or IntelliJ.
The pom.xml has the following structure:
We added spring-boot-starter-web dependency and used spring-boot-maven-plugin plugin to create a jar executable file in /target folder.
5. Project classes
5.1. Create FileService for managing files
The FileService will be responsible for saving and downloading files from the path provided in application.properties. We used a method annotated with @PostConstruct that will create an empty directory (if it does not exist already) at the start of the Spring Boot server.
5.2. Create Model classes: FileData and UploadResponseMessage
The model layer will contain:
FileData - object with field like filename, url(to download file) and size,
UploadResponseMessage - will be used to return information about how uploading process ran.
The UploadResponseMessage class will be used in FilesController and RestExceptionHandler.
5.3. Create FilesController main Rest controller for handing uploading and downloading files
In the controller package, we created the FilesController class that will be responsible for handing all POST, GET, and DELETE requests to /files endpoint.
This class has some interesting annotations:
@RestController - annotation is used to define a Rest controller,
@GetMapping, @PostMapping and @DeleteMapping - annotation is for handing HTTP GET, POST and DELETE requests with specific class methods:
HTTP Method
Endpoint
Method
POST
/files
uploadFile()
GET
/files
getListFiles()
GET
/files/{filename:.+}
getFile()
DELETE
/files
delete()
5.4. Handle upload file exceptions
The class annotated with @ControllerAdvice is responsible for handling specific exceptions that may occur during uploading/downloading files. RestExceptionHandler class beside the special annotation should also extend RestExceptionHandler. To handle the exception when uploading too large files we need to handle MaxUploadSizeExceededException like in the following:
Note that we could also handle other exceptions that may occur during processing requests.
5.5. Create application.properties
To define the maximum file size that could be uploaded we need to add the following entries in the application.properties:
spring.servlet.multipart.max-file-size - this is the maximum file size for each request,
spring.servlet.multipart.max-request-size - the maximum request size for a multipart/form-data.
Additionally, we added the upload.path custom parameter to define our root folder where all the files will be uploaded. This parameter is used in the FileService class.
6. Test Spring Boot upload file application
Find upload-file-into-filesystem-0.0.1-SNAPSHOT.jar in the target folder and Start Spring Boot application by running java -jar upload-file-into-filesystem-0.0.1-SNAPSHOT.jar.
You should see a message similar to this: Started Application in 1.625 seconds (JVM running for 1.98). That means the server started successfully.
To test our uploading/downloading API we used Postman.
6.1. Uploding file
Uploading files returns information about the wrapped in UploadResponseMessage object.
6.2. Get file list
6.3. Delete all files
Deleting files will just return HTTP 200 status when successful.
6.4. Empty list after deleting all files
When the upload folder is empty we will get an empty collection on GET request.
7. Conclusion
In this tutorial, we presented how to create a simple Spring Boot application for uploading and downloading files to/from a static folder located somewhere in the filesystem. The application provides a Rest API without any front-end, that's why we tested how it works using Postman tool.
As usual code used in this tutorial is available on our GitHub repository.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}