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.

Programmer DD
Programmer DD
Programmer DD
Upload Multiple Files with Spring Boot – A Complete Step-by-Step Guide

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/

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaSpring Bootfile uploadMultipartFilemultiple files
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.