In this tutorial we are going to present how to create an application for uploading a single file into filesystem using Spring Boot 2 and Angular 11.
Let's take a look at the architecture:
Here we have a general division into the backend and frontend side. The Angular in version 11 will be responsible for building the UI and the Spring Boot will handle uploaded files and store them into the filesystem. The communication between modules will be RESTful.
3. Spring Boot application
On the backend side, we have a Spring Boot application server that provides a single endpoint /files for handling multipart/form-data requests.
3.1. Project structure
The backend application will have the following structure:
Let's briefly explain used components:
Application - main Spring Boot class responsible for starting web container,
FilesController - Spring REST controller class for handling HTTP requests,
FileUploadException - base exception class,
RestExceptionHandler - class responsible for mapping exceptions into HTTP responses,
UploadResponseMessage - Java representation of JSON that provides information about uploading status,
FileService - service that stores uploaded files in the filesystem,
application.properties - Spring Boot application configuration file.
3.2. Rest Controller
The FilesController class is responsible for handling multipart requests.
In this class, we used the MultipartFile object that is a representation of an uploaded file received in a multipart request. When the user uploaded a file, at the first step it will be stored in a temporary location in the filesystem and the MultipartFile points to that file. This temporary file will be removed at the end of request processing.
3.3. Service class
The FileService is responsible for saving uploaded files in the filesystem:
Path, where we will store all uploaded files, is saved in application parameters in property upload.path. If a file with the same name exists in that location the new FileUploadException will be thrown.
3.4. Spring Boot application class
In the Spring Boot application class we configured CORS and upload size limits just to show it could be implemented here:
3.5. Application configuration file
In the application properties file we have a single entry with the path to the folder where we will save all files:
4. Angular application
To generate the basic structure of the Angular application we will use CLI:
We used the following command:
Next, we would like to create a service and component for uploading files:
4.1. Web project structure
The generated Angular application project has the following structure:
In this structure we could distinguished:
app.module - used to import necessary libraries and components,
upload-file.service - method to upload files in Spring Boot application server, used HTTPClient,
upload-files.component - the main website that contains upload form,
app.component - main application component,
index.html - base HTML website.
4.2. Upload file service
The UploadFileService will use the HTTPClient library to send multipart requests to the backend.
The component used FormData is a special data structure used to save key/value pairs. It uses the same format a form would use with the encoding set to multipart/form-data.
4.3. Upload file component
The UploadFileComponent will be our main component responsible for interactions with the end-user.
The selectFile method will be called then the user selects the file in the input field.
On upload file action the upload(): function will be called. It uses FileService to send multipart data to the backend side. When the parameter reportProgress: true, is set in HTTPClient we could present percent of uploaded file:
In component HTML we have a single input with special type="file". Button Upload file is used to send file data to Spring Boot server:
4.4. Application module
In Application module we import HTTPClientModule used in FileService and declare two components:
The following screencast presents uploading file functionality:
In this tutorial, we presented how to build a simple application for uploading a file using Angular 11 on the frontend side and Spring Boot 2 on the backend.