1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| private static final String[] ALLOWED_EXTENSIONS =
{"jpg", "png", "pdf", "txt"};
// 파일명 정규화
private static String normalizeFilename(String filename) {
if (filename == null) return null;
String name = URLDecoder.decode(filename, StandardCharsets.UTF_8);
name = Normalizer.normalize(name, Normalizer.Form.NFC);
name = name.replace("\0", ""); // NULL 바이트 제거
name = name.replaceAll("[<>:\"/\\\\|?*]", "");
name = name.replaceAll("^[.\\s]+|[.\\s]+$", "");
return name;
}
// 확장자 추출 및 이중 확장자 차단
private static String getExtension(String filename) {
String safe = normalizeFilename(filename);
int dotCount = safe.length() - safe.replace(".", "").length();
// 이중 확장자 차단
if (dotCount != 1) return "";
int idx = safe.lastIndexOf('.');
if (idx == -1) return "";
return safe.substring(idx + 1).toLowerCase();
}
|