9.1. 파일 업로드 표준
9.1.1. 파일 크기 제한
application.yml에 파일 업로드 크기 제한을 설정합니다.
yaml
spring:
servlet:
multipart:
max-file-size: 10MB
max-request-size: 50MB| 설정 | 기본값 | 설명 |
|---|---|---|
max-file-size | 10MB | 단일 파일 최대 크기 |
max-request-size | 50MB | 요청 전체 최대 크기 (다중 파일 포함) |
9.1.2. 허용 확장자 / MIME 타입 검증
파일 확장자와 MIME 타입을 모두 검증해야 합니다. 확장자만 검증하면 위조가 가능합니다.
java
@ConfigurationProperties(prefix = "app.storage")
public record StorageProperties(
String uploadPath,
long maxFileSize,
List<String> allowedExtensions,
List<String> allowedMimeTypes
) {}yaml
app:
storage:
upload-path: /data/uploads
max-file-size: 10485760
allowed-extensions:
- pdf
- png
- jpg
- jpeg
- gif
- xlsx
- docx
allowed-mime-types:
- application/pdf
- image/png
- image/jpeg
- image/gif
- application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
- application/vnd.openxmlformats-officedocument.wordprocessingml.document9.1.3. 파일명 처리
업로드된 파일은 UUID 기반 저장명으로 저장하고, 원본 파일명은 메타데이터로 보존합니다.
java
public class FileNameGenerator {
public static String generate(String originalFilename) {
String extension = extractExtension(originalFilename);
return UUID.randomUUID() + "." + extension;
}
private static String extractExtension(String filename) {
int lastDot = filename.lastIndexOf('.');
if (lastDot == -1) {
throw new InvalidFileException("확장자가 없는 파일은 업로드할 수 없습니다.");
}
return filename.substring(lastDot + 1).toLowerCase();
}
}| 항목 | 규칙 |
|---|---|
| 저장 파일명 | UUID + 확장자 (550e8400-e29b-41d4-a716-446655440000.pdf) |
| 원본 파일명 | DB에 별도 저장 |
| 확장자 | 소문자로 통일 |