java
CHAPTER 60 / 69
읽기 약 2분
FUNCTION
파일 업로드
핵심 개념
Spring에서 이미지/파일 업로드를 구현한다. MultipartFile 처리, 로컬/S3 저장, 파일 형식 검증, 용량 제한 패턴을 익힌다.
코드 분석
# application.properties
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
file.upload.path=/uploads
// FileController.java
@RestController
@RequestMapping("/api/files")
@RequiredArgsConstructor
public class FileController {
private final FileService fileService;
@PostMapping("/upload")
public ApiResponse<String> upload(
@RequestParam("file") MultipartFile file) {
String url = fileService.upload(file);
return ApiResponse.ok(url);
}
}
// FileService.java
@Service
public class FileService {
private final List<String> ALLOWED_TYPES =
List.of("image/jpeg", "image/png", "image/webp");
public String upload(MultipartFile file) {
// 검증
if (file.isEmpty()) throw new BadRequestException("파일이 없습니다");
if (!ALLOWED_TYPES.contains(file.getContentType()))
throw new BadRequestException("허용되지 않는 파일 형식");
if (file.getSize() > 10 * 1024 * 1024)
throw new BadRequestException("10MB 초과");
String filename = UUID.randomUUID() + "_" +
StringUtils.cleanPath(file.getOriginalFilename());
Path path = Paths.get(uploadPath, filename);
Files.copy(file.getInputStream(), path,
StandardCopyOption.REPLACE_EXISTING);
return "/uploads/" + filename;
}
}AI 프롬프트
🤖 AI에게 잘 물어보는 법 — 모델·전략별 프롬프트
Claude
무료: Sonnet 4.6 / Pro $20/mo: Opus 4.6
이 Spring '파일 업로드' 코드에서 DI 관련 버그·순환 참조·트랜잭션 누수를 찾아서 수정해줘.
ChatGPT
무료: GPT-5.5 / Plus $20/mo: GPT-5.5 Pro
'파일 업로드'를 Spring Boot 3.x로 구현한 실전 API 코드(컨트롤러+서비스+레포지토리+테스트)를 완성형으로 만들어줘.
Gemini
무료: 2.5 Flash / Pro $19.99/mo: 3.1 Pro
이 Spring '파일 업로드' 프로젝트의 빈 구조와 의존성 트리를 전체 분석하고 N+1 쿼리·순환 참조·성능 병목을 정리해줘.
Grok
무료: Grok 4.1 / SuperGrok $30/mo
Spring '파일 업로드' vs Quarkus·Micronaut·Ktor의 동일 기능 구현을 2026년 한국 채용 시장 기준으로 솔직히 비교해줘.
⭐ 이것만 기억하세요
파일 업로드는 이 3가지만 확실히 잡으세요
1.파일을 서버 로컬에만 저장하면 서버 확장 시 파일이 분산돼서 관리가 안 됩니다
2.MultipartFile로 받고, S3/MinIO에 저장하며, 파일 크기·형식 제한으로 보안을 확보합니다
3.다음 챕터에서 서버에서 이메일을 발송합니다
공유하기
진행도 60 / 69