Upload Download Files#
Introduction#
- In this topic we will learn about  upload and download files to/from database with a Spring Boot Rest APIs. We also use Spring WebÂ
MultipartFile
 interface to handle HTTP multi-part requests.
Basic Example With Jpa And Postgres#
-
Our Spring Boot Application will provide APIs for:
- uploading File to PostgreSQL/MySQL database
- downloading File database with the link
- getting list of Files’ information (file name, url, type, size)
-
These are APIs to be exported:
Methods | Urls | Actions |
---|---|---|
POST | /v1/files | upload a File |
GET | /v1/files | get List of Files (name, url, type, size) |
GET | /v1/files/{id} | download a File by fileId |
- The uploaded files will be stored in PostgreSQL Database files table with these fields as in the entity below.
FileDB | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
Dependency#
- Firstly, let's add some dependencies as below for Spring Boot, Spring Data Jpa, Postgresql and Tika. Apache Tika is a toolkit for detecting and extracting metadata and structured text content from various documents using existing parser libraries.
pom.xml | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
Entity#
- Then let's define a entity which isÂ
FileDB
which contains fields as below.
FileDB | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
Repository#
- Next, lets create a repository for the entity above.
1 2 3 4 5 6 7 8 9 10 11 |
|
Model#
- Then let's create some DTOs for using in Controller and Service as below.
FileDto.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
FileContentDto.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Config#
- Let's create an
ApplicationProperty.java
config as below which will load the configuration for Spring Boot server url and api path for downloading file. We will use these information for generating download file url.
ApplicationProperty.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
- Now, let's add configuration for our
application.yaml
as below.
application.yaml | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Service#
- Now let's create a Service and export 3 methods for
saveFile
,getFileDate
andgetFilesInformation
as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
saveFile
method saves a file to the database. It checks for a non-null original filename, cleans the filename usingStringUtils.cleanPath
, and creates aFileDB
entity to store information such as filename, file extension, content type, and file data.getFileData
method retrieves the content of a file by its unique identifier (UUID
). It returns aFileContentDto
containing the filename and file content.getFilesInformation
method retrieves information about all stored files in the database. It returns a list ofFileDto
objects containing details such as file ID, filename, file type, creation date, update date, file extension, and file location.- Helper Methods:
mapToFileDto
: Maps aFileDB
entity to aFileDto
.mapToFileContentDto
: Maps aFileDB
entity to aFileContentDto
.buildLocationUrl
: Builds the URL for accessing a file based on its ID.
Controller#
- Finally, let's create a controller for exporting apis as we mentioned in the beginning.
UploadDownloadFileController.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
-
uploadFile
endpoint handles the file upload operation. It takes aMultipartFile
as a request parameter and returns aFileDto
in the response body. The file is saved using thefileStorageService.saveFile
method. -
downloadFile
endpoint handles the file download operation. It takes the file ID as a path variable and returns aResponseEntity
containing the file content as bytes. TheContent-Disposition
header is set to indicate that the response should be treated as an attachment. -
getFilesInformation
endpoint retrieves information about all stored files and returns a list ofFileDto
objects in the response body. -
Helper Methods:
- There are no explicit helper methods in the controller. The logic for file storage operations is delegated to the
FileStorageService
class.
- There are no explicit helper methods in the controller. The logic for file storage operations is delegated to the
Testing#
-
Now, let's start our Spring Boot service and use Postman to test exported apis as below.
-
For calling upload file api with postman let's choose
body
withform-data
. Then put the key and select the type isFile
then we can click on the value column and chooseNew file from local machine
and you can select a file from your local machine.
- After the call is successful we can see the response contains some information and the link for getting our file.
- Next, let's copy this link and put it on any browser then you can download it immediately.
- Finally, for getFilesInformation api, we will have the result as below.