251211_01_ENH : FileUtil 추가
This commit is contained in:
54
src/main/java/com/kri/common/file/FileUtil.java
Normal file
54
src/main/java/com/kri/common/file/FileUtil.java
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user