1

Yesterday, we introduced how to upload files . Some readers ask: What if there are multiple files to upload at the same time? This will immediately provide a method for handling multiple files to be uploaded at the same time when encountering multiple files.

Try it

The hands-on part of this article will be based on document uploading in the Spring Boot, so readers can take the example of the previous article as a basis for modification to understand the difference between these, and the following will mainly explain the core difference The place.

first step : modify the upload form of the file 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>

You can see that one more input file input box is added here, and the name of the file input box is modified to files. Because there are multiple files, a plural number is used. Note: The names of these input boxes are the same, so that they can be organized into an array when processing files in the backend.

second step : modify the back-end processing interface

@PostMapping("/upload")
@ResponseBody
public String create(@RequestPart MultipartFile[] files) throws IOException {
    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();
}

Several important changes:

  1. 060d8aea2b705f uses an array, the parameter name files corresponds to the name MultipartFile
  2. The main body of the subsequent processing file (in the for loop) is the same as before, that is, the MultipartFile array is stored for each file in a loop traversal method, and then the splicing result returns the information.

More free tutorials in this series "Click to enter the summary directory"

Test verification

first step : Start the Spring Boot application, visit http://localhost:8080 , you can see the following file upload page.

Second step : Select 2 files not larger than 2MB, and click the "Submit" button to complete the upload.

If the upload is successful, a page similar to the following will be displayed:

You can check whether the file is actually uploaded according to the printed file path.

Code example

For related examples in this article, you can view the chapter4-4 directory in the following warehouse:

If you think this article is good, welcome Star support, your attention is my motivation for persistence!


jafeney
926 声望885 粉丝

成功的道路并不拥挤,因为坚持的人不会太多