<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title>upload webpage</title>
</head>
<body>
<h1>upload webpage</h1>
<form method="post" action="/upload" enctype="multipart/form-data">
file 1:<input type="file" name="files"><br>
file 2:<input type="file" name="files"><br>
<hr>
<input type="submit" value="submit">
</form>
</body>
</html>
@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();
}
코드를 살펴보면 단일 파일 업로드 할때와 다른 점은 다수 파일을 받아야 되다보니 MultipartFile을 배열로 받아야 한다. 그리고 file html 코드의 input 태그 name 이랑 일치해야 데이터 바인딩이 가능하다.