Get the File Path of Upload File
ane. Introduction
In this article, we will show how to upload a file with Spring Boot to a binder located in the filesystem. Nosotros will use Spring MultipartFile
interface to handle multi-part requests to our Balance API.
2. Application Balance uploading API
Spring Boot application server will provide API with the following endpoints:
URL | Method | Activeness |
/files | GET | Get list of uploaded files |
/files | Mail service | Upload a single file |
/files | DELETE | Delete all uploaded files |
/files/{filename} | Become | Download specific file |
Files will be uploaded into the specific static binder which volition be configured in the application.properties
.
3. Projection 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, -
Awarding
- the main Leap 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
- handlesMaxUploadSizeExceededException
when processing file (could be extended with other exceptions as well), -
FileData
- contains information about the uploaded file like proper name, size, location, -
application.properties
- Spring Boot awarding properties with a path for uploaded files.
4. Setup Bound Boot application
To create a Leap Boot projection from scratch we could apply 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 bound-boot-maven-plugin
plugin to create a jar executable file in /target
folder.
v. Project classes
v.1. Create FileService
for managing files
The FileService
will exist 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 non be already) at the start of the Jump Kick server.
5.two. Create Model classes: FileData
and UploadResponseMessage
The model layer will contain:
-
FileData
- object with field like filename, url(to download file) and size, -
UploadResponseMessage
- volition be used to render information about how uploading process ran.
The UploadResponseMessage
class volition be used in FilesController
and RestExceptionHandler
.
5.3. Create FilesController
main Rest controller for handing uploading and downloading files
In the controller package, nosotros created the FilesController
class that will be responsible for handing all POST, GET, and DELETE requests to /files
endpoint.
This grade has some interesting annotations:
-
@RestController
- annotation is used to ascertain a Residual controller, -
@GetMapping
,@PostMapping
and@DeleteMapping
- annotation is for handing HTTP Get, Postal service and DELETE requests with specific class methods:
HTTP Method | Endpoint | Method |
/files | uploadFile() | |
Get | /files | getListFiles() |
Go | /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 besides extend RestExceptionHandler
. To handle the exception when uploading too big files we need to handle MaxUploadSizeExceededException
similar in the following:
Note that we could also handle other exceptions that may occur during processing requests.
5.5. Create application.backdrop
To ascertain the maximum file size that could exist uploaded we need to add the post-obit 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 asking size for a multipart/form-data.
Additionally, nosotros 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
grade.
6. Test Leap Kick upload file application
Discover upload-file-into-filesystem-0.0.1-SNAPSHOT.jar
in the target
folder and Start Leap Kicking application by running coffee -jar upload-file-into-filesystem-0.0.i-SNAPSHOT.jar
.
Yous should come across a message like to this: Started Awarding in 1.625 seconds (JVM running for one.98)
. That ways the server started successfully.
To examination our uploading/downloading API we used Postman.
6.ane. Uploding file
Uploading files returns data well-nigh the wrapped in UploadResponseMessage
object.
six.ii. Get file listing
six.3. Delete all files
Deleting files will just render HTTP 200 status when successful.
six.4. Empty list afterward deleting all files
When the upload folder is empty nosotros will go an empty collection on Go asking.
7. Conclusion
In this tutorial, nosotros presented how to create a simple Leap Boot application for uploading and downloading files to/from a static folder located somewhere in the filesystem. The awarding provides a Residue API without any front-end, that's why we tested how information technology works using Postman tool.
As usual lawmaking used in this tutorial is available on our GitHub repository.
gentileconstainey.blogspot.com
Source: https://frontbackend.com/spring-boot/spring-boot-upload-file-to-filesystem