From e3700aabffd59934325b565957a0535e66fa790e Mon Sep 17 00:00:00 2001 From: kric_jspark Date: Thu, 11 Dec 2025 14:36:05 +0900 Subject: [PATCH] =?UTF-8?q?251211=5F01=5FENH=20:=20FileUtil=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/kri/common/file/FileUtil.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/main/java/com/kri/common/file/FileUtil.java diff --git a/src/main/java/com/kri/common/file/FileUtil.java b/src/main/java/com/kri/common/file/FileUtil.java new file mode 100644 index 0000000..17f61fc --- /dev/null +++ b/src/main/java/com/kri/common/file/FileUtil.java @@ -0,0 +1,54 @@ +package com.kri.common.file; + +import java.util.regex.Pattern; + +public final class FileUtil { + private static final Pattern FILENAME_SANITIZE = Pattern.compile("[^a-zA-Z0-9._-]"); + + /** + * 파일명에서 위험한 문자 제거 + */ + public static String sanitizeFileName(String original) { + if (original == null || original.isBlank()) { + return "file"; + } + String name = original.trim().replace("\\", "/"); + int idx = name.lastIndexOf('/'); + if (idx >= 0 && idx < name.length() - 1) { + name = name.substring(idx + 1); + } + name = FILENAME_SANITIZE.matcher(name).replaceAll("_"); + // 너무 짧으면 기본값 + if (name.isBlank()) { + return "file"; + } + return name; + } + + public static String getExtension(String fileName) { + if (fileName == null) return ""; + int dot = fileName.lastIndexOf('.'); + if (dot < 0 || dot == fileName.length() - 1) { + return ""; + } + return fileName.substring(dot + 1); + } + + public static String extractFileNameFromPath(String pathLike) { + if (pathLike == null || pathLike.isBlank()) return ""; + String s = pathLike; + int q = s.indexOf('?'); + if (q >= 0) { + s = s.substring(0, q); + } + int hash = s.indexOf('#'); + if (hash >= 0) { + s = s.substring(0, hash); + } + int slash = s.lastIndexOf('/'); + if (slash >= 0 && slash < s.length() - 1) { + return s.substring(slash + 1); + } + return s; + } +}