SPRING/스프링 MVC
핸들러 메소드 13부: MultipartFile
JUMP개발자
2021. 7. 9. 19:13
MultipartFile
- 파일 업로드시 사용하는 메소드 아규먼트
- MultipartResolver 빈이 설정 되어 있어야 사용할 수 있다.
- POST multipart/form-data 요청에 들어있는 파일을 참조할 수 있다.
- List<MultipartFile> 아큐먼트로 여러 파일을 참조할 수도 있다.
@Controller
public class FileController {
@GetMapping("/file")
public String fileUploadForm() {
return "files/index";
}
@PostMapping("/file")
public String fileUpload(@RequestParam MultipartFile file, RedirectAttributes attributes) {
// Save 로직
String message = file.getOriginalFilename() +" is uploaded";
attributes.addFlashAttribute("message", message);
return "redirect:/file";
}
}
file upload 기능 front단
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:if="${message}">
<h2 th:text="${message}"/>
</div>
<form method="POST" enctype="multipart/form-data" action="#" th:action="@{/file}">
File: <input type="file" name="file"/>
<input type="submit" value="Upload"/>
</form>
</body>
</html>
결과 : DASP.jpg 파일을 업로드한 후