Upload Multiple Files with Spring Boot – A Complete Step-by-Step Guide
This tutorial shows how to modify a Spring Boot file‑upload page and controller to handle multiple files, detailing HTML form changes, backend code using MultipartFile arrays, and verification steps with screenshots for testing.
Yesterday we introduced how to implement file upload in Spring Boot (link). Readers asked about uploading multiple files, so here's the solution.
Hands‑on Demo
This hands‑on part builds on the previous Spring Boot file‑upload example (link). Use the earlier example as a base and modify it as described.
Step 1: Update the upload page
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title>文件上传页面 - didispace.com</title>
</head>
<body>
<h1>文件上传页面</h1>
<form method="post" action="/upload" enctype="multipart/form-data">
文件1:<input type="file" name="files"><br>
文件2:<input type="file" name="files"><br>
<hr>
<input type="submit" value="提交">
</form>
</body>
</html>We added extra input elements, giving them the same name files so the backend receives an array.
Step 2: Update the backend controller
@PostMapping("/upload")
@ResponseBody
public String create(@RequestPart MultipartFile[] files) {
StringBuffer message = new StringBuffer();
for (MultipartFile file : files) {
String fileName = file.getOriginalFilename();
String filePath = path + fileName;
File dest = new File(filePath);
Files.copy(file.getInputStream(), dest.toPath());
message.append("Upload file success : " + dest.getAbsolutePath())
.append("<br>");
}
return message.toString();
}Key changes: use a MultipartFile[] array matching the files input name; loop through the array to store each file and build a response.
Testing
Step 1: Run the Spring Boot app and visit http://localhost:8080
Step 2: Choose two files (≤2 MB) and click “Submit”.
If successful, a page shows the uploaded file paths.
Code Repository
GitHub: https://github.com/dyc87112/SpringBoot-Learning/
Gitee: https://gitee.com/didispace/SpringBoot-Learning/
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
