경로에 파일 저장하는 코드 2가지
1. MultipartFile의 transferTo() - 내부적으로 FileCopyUtils의 copy()를 사용함 !
2. FileCopyUtils의 copy(getInputStream(), Files.newOutputStream())
예)
private final String directory = "C:\\app\\eGovFrameDev-4.0.0-64bit\\workspace\\fitness-management\\src\\main\\webapp\\resources\\images\\profile";
@PostMapping("/register")
public String register(@Valid @ModelAttribute("userRegisterForm") UserRegisterForm userRegisterForm, BindingResult errors) throws IOException {
MultipartFile upfile = userRegisterForm.getUpfile();
if(!upfile.isEmpty()) {
String filename = upfile.getOriginalFilename();
userRegisterForm.setPhoto(filename);
// 1. MultipartFile의 transferTo()
upfile.transferTo(new File(directory, filename));
// 2. FileCopyUtils의 copy()
FileCopyUtils.copy(upfile.getInputStream(), new FileOutputStream(new File(directory, filename)));
}
'Spring' 카테고리의 다른 글
| [인프런/스프링 DB 1편] 2. 커넥션풀과 데이터소스 (0) | 2023.06.19 |
|---|---|
| [인프런/스프링 DB 1편] 1. JDBC (0) | 2023.06.17 |
| [인프런/스프링 MVC 2편] 11. 파일 업로드, 다운로드 (0) | 2023.06.02 |
| [인프런/스프링 MVC 2편] 10. 스프링 타입 컨버터 (0) | 2023.05.31 |
| [인프런/스프링 MVC 2편] 9. API 예외 처리 (1) | 2023.05.31 |