まず、upload.htmlページを作成して、複数ファイルのアップロードの操作をシミュレートします。内容は次のとおりです。
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>多文件上传</title>
</head>
<body>
<form th:action="@{/upload}" method="post" enctype="multipart/form-data">
照片1:<input type="file" name="photo"/><br/>
照片2:<input type="file" name="photo"/><br/>
照片3:<input type="file" name="photo"/><br/>
<input type="submit" value="上传">
</form>
</body>
</html>
アップロードされたコントローラークラスUploadControllerの情報は次のとおりです。
package com.springboot.advance.controller;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Controller
public class UploadController {
@GetMapping("/upload_html")
public String uploadHtml() {
return "upload";
}
@PostMapping("/upload")
@ResponseBody
public String upload(HttpServletRequest httpRequest) {
if (httpRequest instanceof MultipartHttpServletRequest) {
MultipartHttpServletRequest request = (MultipartHttpServletRequest) httpRequest;
List<MultipartFile> photos = request.getFiles("photo");
}
return null;
}
}
次のように、ページのhttp:// localhost:8080 / upload_htmlにアクセスし、アップロードページにアクセスして操作します。
[アップロード]をクリックし、コントローラーのアップロードメソッドにブレークポイントを設定して、次のようにパラメーターを確認します。
バックグラウンドはすでに複数のファイルの入力パラメーターを受信できることがわかります。次に、独自のビジネスロジックに従って操作します。