Yesterday, we introduced how to implement file upload Spring Boot. 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 the Spring Boot to realize file upload , 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:
- 0615b08ee7a78e uses an array, the parameter name files corresponds to the name
MultipartFile
- 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 are serialized "Click to enter the summary catalog"
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:
- Github:https://github.com/dyc87112/SpringBoot-Learning/
- Gitee:https://gitee.com/didispace/SpringBoot-Learning/
If you think this article is good, welcome Star
support, your attention is my motivation for persistence!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。