I will be using Spring Initialzr to generate my Spring Boot application, include dependencies while creating a project. Also, I will be using the MySQL database to keep track of files uploaded and their metadata.

This API will be helpful for those who are registering new users to their business/application and want users to upload documents for verification.

Spring Boot dependencies

Set up application.properties file with the database, upload the directory, and other details:

1
2
4
6
7
16
17
18
20
22
23
24

Create an entity class, DocumnentStorageProperties.java, to save information about a file uploaded and to avoid the duplication of files (you can skip this if you don’t want to keep metadata).

Here I am keeping my database name as merchant and table name as merchant_documents. There’s no need to create the table manually as I have set up spring.jpa.generate-ddl=true in properties. It will fire the DDL command when the application starts to create a table according to the Entity class defined in Java.

Java
3
6
23
24

In the above entity class, the upload_dir variable will get initialized by the value which we have set in application.properties(file.upload-dir).

Create a repository interface for the above entity to get built-in CRUD operations method support. Add our own new method to check whether a particular file is present for that user or not.

Java
1
2
3
7
8
10
11
12

Create a Service class to store and download files on the server, and to store information in the database. Here I have used a few more new classes which I will be explaining in a later section of this article, so if you get any compilation issues, ignore them for the moment.

Java
13
24
25
29
30
31
37
41
42
43
47
48
52
53
54
55
57
59
64
66
73
74
80
81
85
87
88
93

There are two important methods in the Service class; one is  storeFilewhich will store the file at the required location and save the file details in the database. Before storing the file, we will check whether a user has already given the type of file available, and if it is available, then replace the latest file on a server and update the database with the latest information. Otherwise, it will create a new entry in the database and save the file at the required location. We will be keeping the file name as  <userId>_<docType>.<extension> just to avoid ambiguity in the file name.

The second method is  loadFileAsResource which returns the file as a Resource on the basis of file name given.

Also, we have an autowired repository object to this service which we have created in thr previous step and autowired  fileStorageLocation  on basis of upload-dir properties.

Now let’s create a controller that will handle the HTTP request and send the proper response back.

Java
6
8
10
15
24
27
35
38
44
46
50
51
53
55
56
57
58
59
63
64
67

So the above controller has two mappings:

Other Classes which we have used are:

 UploadFileResponse: To send the response back when the upload is successful.

 DocumentStorageException: Our own Custom exception type

7

In order to secure your API with Spring Basic Auth add below the class. Update the required user name and password (use online Bcrypt encoder to encode your password).

1
2
3
7
8
9
10
11
12
19
24
26
30
32
33

Post request to upload a file:

Request to upload a file

GET request to download the file:

Request to dowload file

Verify that file is visible at the required location.

This content was originally published here.