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.
Set up application.properties file with the database, upload the directory, and other details:
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.
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.
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.
There are two important methods in the Service class; one is
storeFile, which 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.
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
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).
Post request to upload a file:
GET request to download the file:
Verify that file is visible at the required location.
This content was originally published here.