1️⃣ 파일 처리 개요

정의

Spring 파일 처리는 클라이언트가 파일을 업로드하거나 서버가 파일을 내려주는 기능입니다.

“Multipart 요청을 처리하고, 파일을 안전하게 저장·전송”


2️⃣ 파일 업로드 기본 구조 ⭐⭐⭐

Client (Form / Axios)
 ↓ multipart/form-data
Controller
 ↓ MultipartFile
Service
 ↓
File System / Cloud Storage

3️⃣ MultipartFile이란

정의

MultipartFile업로드된 파일을 Spring이 추상화한 객체입니다.

org.springframework.web.multipart.MultipartFile

주요 메서드

메서드 설명
getOriginalFilename() 원본 파일명
getSize() 파일 크기
getContentType() MIME 타입
isEmpty() 파일 존재 여부
transferTo(File) 파일 저장

4️⃣ 파일 업로드 Controller 예제 ⭐⭐⭐

@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("업로드 성공");
}

5️⃣ 업로드 설정 (필수)