Spring 파일 처리는 클라이언트가 파일을 업로드하거나 서버가 파일을 내려주는 기능입니다.
“Multipart 요청을 처리하고, 파일을 안전하게 저장·전송”
Client (Form / Axios)
↓ multipart/form-data
Controller
↓ MultipartFile
Service
↓
File System / Cloud Storage
MultipartFile은 업로드된 파일을 Spring이 추상화한 객체입니다.
org.springframework.web.multipart.MultipartFile
| 메서드 | 설명 |
|---|---|
| getOriginalFilename() | 원본 파일명 |
| getSize() | 파일 크기 |
| getContentType() | MIME 타입 |
| isEmpty() | 파일 존재 여부 |
| transferTo(File) | 파일 저장 |
@PostMapping("/upload")
public ResponseEntity<String> upload(
@RequestParam MultipartFile file) throws IOException {
if (file.isEmpty()) {
return ResponseEntity.badRequest().body("파일 없음");
}
File dest = new File("uploads/" + file.getOriginalFilename());
file.transferTo(dest);
return ResponseEntity.ok("업로드 성공");
}