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Â
FileDBwhich 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.javaconfig 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.yamlas 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,getFileDateandgetFilesInformationas 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 | |
saveFilemethod saves a file to the database. It checks for a non-null original filename, cleans the filename usingStringUtils.cleanPath, and creates aFileDBentity to store information such as filename, file extension, content type, and file data.getFileDatamethod retrieves the content of a file by its unique identifier (UUID). It returns aFileContentDtocontaining the filename and file content.getFilesInformationmethod retrieves information about all stored files in the database. It returns a list ofFileDtoobjects containing details such as file ID, filename, file type, creation date, update date, file extension, and file location.- Helper Methods:
mapToFileDto: Maps aFileDBentity to aFileDto.mapToFileContentDto: Maps aFileDBentity 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 | |
-
uploadFileendpoint handles the file upload operation. It takes aMultipartFileas a request parameter and returns aFileDtoin the response body. The file is saved using thefileStorageService.saveFilemethod. -
downloadFileendpoint handles the file download operation. It takes the file ID as a path variable and returns aResponseEntitycontaining the file content as bytes. TheContent-Dispositionheader is set to indicate that the response should be treated as an attachment. -
getFilesInformationendpoint retrieves information about all stored files and returns a list ofFileDtoobjects 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
FileStorageServiceclass.
- 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
bodywithform-data. Then put the key and select the type isFilethen we can click on the value column and chooseNew file from local machineand 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.



