-
핸들러 메소드 13부: MultipartFileSPRING/스프링 MVC 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 파일을 업로드한 후
'SPRING > 스프링 MVC' 카테고리의 다른 글
핸들러 메소드 15부: @RequestBody & HttpEntity (0) 2021.07.10 핸들러 메소드 14부 ResponseEntity 예제:) 다운로드 (0) 2021.07.10 핸들러 메소드 12부 - FlashAttributes (0) 2021.07.09 핸들러 메소드 11부 RedirectAttributes (0) 2021.07.08 핸들러 메소드 10부: @SessionAttribute (0) 2021.07.05