In this tutorial, we are going to learn how to create a simple Angular application for downloading files using the Spring Boot on the backend side.
2. Architecture
Let's take a look at the architecture of our sample application.
In the presented solution we have a simple division into the frontend and backend layer. Angular based frontend will be responsible for requesting the backend and downloading files in browser. The communication will be handled using HTTP. In order to integrate both sides of the communication, we need an HTTP client on the frontend side and a REST controller on the backend.
3. Spring Boot application
The Spring Boot application provides an HTTP API with endpoints used for listing files and downloading them.
3.1. Project structure
The structure of the backend project will be as follows:
Here we can distinguished several classes:
Application - main Spring Boot class responsible for starting web container,
FileController - Spring Rest Controller class used for handing HTTP requests,
FileData - simple POJO object used for presenting a list of files (this object will be converted into JSON),
FileService - Service class resopnsible for managing files,
application.properties - Spring Boot configuration file.
3.2. Spring Controller class
The Spring REST controller will handle the following HTTP methods:
URL
HTTP Method
Action
/api/files
GET
Get list files
/api/files/{filename}
GET
Download a file
The Spring Rest Controller class has the following structure:
The controller class is annotated with:
@RestController - this annotation marks class as a REST controller,
@RequestMapping - provide base url for all endpoints mapped in this controller class,
@CrossOrigin - allows cross-domain requests
In order to download a file from the browser without any issue we need to set two headers:
Content-Type - this header holds the type of the file (Files.probeContentType(path) method was used to determine the file type),
Content-Disposition - this header indicates that the file should be downloaded in the browser right away at the moment when the user clicks the link (the 'Save as' dialog will be opened on the browser).
The following fragment of code is used to set up headers:
3.3. Service class
The FileService class is responsible for managing files on the filesystem. It is always a good practice to separate business logic from the REST controller. That's why that class was introduced.
In this implementation, we used UrlResource to return the content of the file from the filesystem, but you can use any other method to read a file in Java and simply return the byte array.
The POJO object is used to return data related to the file such as:
filename - the name of the file,
size - file size,
contentType - file content type.
3.5. Server configuration file
The application.properties contains a single property with a path to the folder where we will search files. This property is used in FileService class.
4. Angular application
The angular application was created using ng commands:
In order to create a basic Angular project structure we used:
To create a file component:
To create a service:
4.1. Web project structure
The generated Angular web project has the following structure:
4.2. Environment variables
All environment-related configuration is located in the angular.json file:
Each environment-specific file is represented by the fileReplacements section.
For example if we use ng build --configuration=production command to build or run the project Angular CLI will replace the environment file src/environments/environment.ts with src/environments/environment.prod.ts.
In our case the environment.prod.ts file looks like this:
Without --configuration=production parameter the regular file will be used environments/environment.ts that contains:
This environment file is used in DownloadService to get the baseUrl: ${environment.baseUrl}/files.
4.3. Angular files component
The main responsibility of FilesComponent is presenting the list of files and downloading a selected file using the file-saver library.
The HTML attached with this component has the following structure:
4.4. Angular download service
The DownloadService is used Angular HttpClient to communicate with the Spring Boot application server. The service retrieves a list of files and downloads the content of the selected file.
4.5. File data model
The FileData object is a Typescript representation of the Java FileData class:
Note we just used a filename from this object, but you could easily use other fields, maybe this is a good exercise?
5. Demo
The application shows the following functionality:
6. Conclusion
In this tutorial, we presented an Angular application with a Spring Boot server for downloading files from the filesystem.
As usual, code used in this tutorial is available on our GitHub repository
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}