In this tutorial, you will learn to build an example to upload multiple files in Spring Boot with MultipartFile

What you'll build

What you'll need

  • JDK 8+ or OpenJDK 8+
  • Maven 3+

Stack

  • Java
  • Spring Boot
  • Freemarker

Init project structure and dependencies

Project structure

├── src
│   └── main
│       ├── java
│       │   └── com
│       │       └── hellokoding
│       │           └── uploadingfiles
│       │               ├── Application.java
│       │               └── UploadingController.java
│       ├── resources
│       │   └── application.properties
│       └── webapp
│           └── uploading.ftl
└── pom.xml

Project dependencies


Define Controller and View Template

Uploading Controller


View Template


<form name="uploadingForm" enctype="multipart/form-data" action="/" method="POST">  

action="/" and method="POST" map with uploadingPost() of UploadingController, enctype="multipart/form-data" works with MultipartFile.

Config and Run

Application Configurations



new File(UploadingController.uploadingdir).mkdirs(); makes a directory for uploading files if not exists.

Run with Maven

Type the below command at the project root directory

mvn spring-boot:run

Source code

https://github.com/hellokoding/hellokoding-courses/tree/master/springboot-examples/springboot-uploading-files