Spring Boot MultipartFile Upload

Following is a quick guide to upload a multipart file in this directory :

/Users/user/uploads

1. Create the Spring boot application:

The application is executed on windows.

Technologies used in this article :

  • Spring boot
  • Maven
  • Intellij

The creation of spring using spring boot and Maven is explained here http://mkaroune.e-monsite.com/pages/spring-boot-hiberante-project/project-creation.html. You do not need to add all the dependencies as shown in the article. Only DevTools, Web and Thymeleaf are needed in this example

Bellow is the project structure.Uploadfileprojectstructure

2.Configuring Server and File Storage Properties:

We will configure the Spring boot application to enable Multipart file download then we will define the max file size. Also, we will configure the directory where the files will be stored.

Open src/main/resources/application.properties file, and add the following properties to it -

3. Binding application properties to a POJO class:

The annotation @ConfigurationProperties enables the application to bind the properties defined in application.properties file to a POJO class. Following is the StorageProperties class.

To enable the Configuration properties, you have to add in the configuration calls.

4. Multipart file upload controller:

The file controller uses the StorageService to store the files to be uploaded.

5. Storing Multipart file service:

NB: Exception classes (StorageFileNotFoundException.java, StorageException.java) were added to handle the different file upload errors.

6.  Demo: Access http://localhost:8080. Then click on add video. Once the file is uploaded, check it in the file defined in the application.properties file.DemopageThis is the link of the application's source code: https://github.com/MarquiseG/Upload-multipart-file

Add a comment