|  | @@ -0,0 +1,205 @@
 | 
	
		
			
				|  |  | +package com.fs.common.utils;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import org.apache.commons.io.FilenameUtils;
 | 
	
		
			
				|  |  | +import java.net.URL;
 | 
	
		
			
				|  |  | +import java.net.MalformedURLException;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +public class FileNameExtractor {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 使用Apache Commons IO获取文件名
 | 
	
		
			
				|  |  | +     * @param urlString URL字符串
 | 
	
		
			
				|  |  | +     * @return 文件名
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static String getFileNameFromUrl(String urlString) {
 | 
	
		
			
				|  |  | +        if (urlString == null || urlString.trim().isEmpty()) {
 | 
	
		
			
				|  |  | +            return null;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            URL url = new URL(urlString);
 | 
	
		
			
				|  |  | +            String path = url.getPath();
 | 
	
		
			
				|  |  | +            return FilenameUtils.getName(path);
 | 
	
		
			
				|  |  | +        } catch (MalformedURLException e) {
 | 
	
		
			
				|  |  | +            // 作为普通路径处理
 | 
	
		
			
				|  |  | +            return FilenameUtils.getName(urlString);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 获取不带扩展名的文件名
 | 
	
		
			
				|  |  | +     * @param urlString URL字符串
 | 
	
		
			
				|  |  | +     * @return 不带扩展名的文件名
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static String getBaseNameFromUrl(String urlString) {
 | 
	
		
			
				|  |  | +        if (urlString == null || urlString.trim().isEmpty()) {
 | 
	
		
			
				|  |  | +            return null;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            URL url = new URL(urlString);
 | 
	
		
			
				|  |  | +            String path = url.getPath();
 | 
	
		
			
				|  |  | +            return FilenameUtils.getBaseName(path);
 | 
	
		
			
				|  |  | +        } catch (MalformedURLException e) {
 | 
	
		
			
				|  |  | +            return FilenameUtils.getBaseName(urlString);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 获取文件扩展名
 | 
	
		
			
				|  |  | +     * @param urlString URL字符串
 | 
	
		
			
				|  |  | +     * @return 文件扩展名
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static String getExtensionFromUrl(String urlString) {
 | 
	
		
			
				|  |  | +        if (urlString == null || urlString.trim().isEmpty()) {
 | 
	
		
			
				|  |  | +            return null;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            URL url = new URL(urlString);
 | 
	
		
			
				|  |  | +            String path = url.getPath();
 | 
	
		
			
				|  |  | +            return FilenameUtils.getExtension(path);
 | 
	
		
			
				|  |  | +        } catch (MalformedURLException e) {
 | 
	
		
			
				|  |  | +            return FilenameUtils.getExtension(urlString);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 从URL或文件路径中获取扩展名
 | 
	
		
			
				|  |  | +     * @param urlOrPath URL字符串或文件路径
 | 
	
		
			
				|  |  | +     * @return 扩展名(不包含点号),如果没有扩展名则返回null
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static String getExtension(String urlOrPath) {
 | 
	
		
			
				|  |  | +        if (urlOrPath == null || urlOrPath.trim().isEmpty()) {
 | 
	
		
			
				|  |  | +            return null;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        String fileName = getFileNameFromUrlOrPath(urlOrPath);
 | 
	
		
			
				|  |  | +        if (fileName == null || fileName.isEmpty()) {
 | 
	
		
			
				|  |  | +            return null;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 查找最后一个点的位置
 | 
	
		
			
				|  |  | +        int lastDotIndex = fileName.lastIndexOf('.');
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 如果没有点,或者点在开头(隐藏文件),或者点在结尾,则没有扩展名
 | 
	
		
			
				|  |  | +        if (lastDotIndex <= 0 || lastDotIndex == fileName.length() - 1) {
 | 
	
		
			
				|  |  | +            return null;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return fileName.substring(lastDotIndex + 1).toLowerCase();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 从URL或文件路径中获取扩展名(包含点号)
 | 
	
		
			
				|  |  | +     * @param urlOrPath URL字符串或文件路径
 | 
	
		
			
				|  |  | +     * @return 扩展名(包含点号),如果没有扩展名则返回null
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static String getExtensionWithDot(String urlOrPath) {
 | 
	
		
			
				|  |  | +        String extension = getExtension(urlOrPath);
 | 
	
		
			
				|  |  | +        return extension != null ? "." + extension : null;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 检查文件是否具有指定的扩展名
 | 
	
		
			
				|  |  | +     * @param urlOrPath URL字符串或文件路径
 | 
	
		
			
				|  |  | +     * @param expectedExtension 期望的扩展名(可以带点号也可以不带)
 | 
	
		
			
				|  |  | +     * @return 是否匹配
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static boolean hasExtension(String urlOrPath, String expectedExtension) {
 | 
	
		
			
				|  |  | +        if (expectedExtension == null) {
 | 
	
		
			
				|  |  | +            return false;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        String actualExtension = getExtension(urlOrPath);
 | 
	
		
			
				|  |  | +        if (actualExtension == null) {
 | 
	
		
			
				|  |  | +            return false;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 处理期望扩展名可能带点号的情况
 | 
	
		
			
				|  |  | +        String cleanExpectedExtension = expectedExtension.startsWith(".") ?
 | 
	
		
			
				|  |  | +                expectedExtension.substring(1) : expectedExtension;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return actualExtension.equalsIgnoreCase(cleanExpectedExtension);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 检查文件是否是图片类型
 | 
	
		
			
				|  |  | +     * @param urlOrPath URL字符串或文件路径
 | 
	
		
			
				|  |  | +     * @return 是否是图片文件
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static boolean isImageFile(String urlOrPath) {
 | 
	
		
			
				|  |  | +        String extension = getExtension(urlOrPath);
 | 
	
		
			
				|  |  | +        if (extension == null) {
 | 
	
		
			
				|  |  | +            return false;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        String[] imageExtensions = {"jpg", "jpeg", "png", "gif", "bmp", "webp", "svg", "ico"};
 | 
	
		
			
				|  |  | +        for (String imgExt : imageExtensions) {
 | 
	
		
			
				|  |  | +            if (extension.equalsIgnoreCase(imgExt)) {
 | 
	
		
			
				|  |  | +                return true;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return false;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 检查文件是否是文档类型
 | 
	
		
			
				|  |  | +     * @param urlOrPath URL字符串或文件路径
 | 
	
		
			
				|  |  | +     * @return 是否是文档文件
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public static boolean isDocumentFile(String urlOrPath) {
 | 
	
		
			
				|  |  | +        String extension = getExtension(urlOrPath);
 | 
	
		
			
				|  |  | +        if (extension == null) {
 | 
	
		
			
				|  |  | +            return false;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        String[] docExtensions = {"pdf", "doc", "docx", "xls", "xlsx", "ppt", "pptx", "txt", "rtf"};
 | 
	
		
			
				|  |  | +        for (String docExt : docExtensions) {
 | 
	
		
			
				|  |  | +            if (extension.equalsIgnoreCase(docExt)) {
 | 
	
		
			
				|  |  | +                return true;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return false;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 从URL或路径中提取文件名的辅助方法
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    private static String getFileNameFromUrlOrPath(String urlOrPath) {
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            java.net.URL url = new java.net.URL(urlOrPath);
 | 
	
		
			
				|  |  | +            String path = url.getPath();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            // 处理查询参数,移除?后面的内容
 | 
	
		
			
				|  |  | +            int queryIndex = path.indexOf('?');
 | 
	
		
			
				|  |  | +            if (queryIndex > 0) {
 | 
	
		
			
				|  |  | +                path = path.substring(0, queryIndex);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if (path.endsWith("/")) {
 | 
	
		
			
				|  |  | +                path = path.substring(0, path.length() - 1);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            int lastSlashIndex = path.lastIndexOf('/');
 | 
	
		
			
				|  |  | +            return lastSlashIndex >= 0 ? path.substring(lastSlashIndex + 1) : path;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        } catch (java.net.MalformedURLException e) {
 | 
	
		
			
				|  |  | +            // 作为普通路径处理
 | 
	
		
			
				|  |  | +            String path = urlOrPath.trim();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            // 处理查询参数
 | 
	
		
			
				|  |  | +            int queryIndex = path.indexOf('?');
 | 
	
		
			
				|  |  | +            if (queryIndex > 0) {
 | 
	
		
			
				|  |  | +                path = path.substring(0, queryIndex);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if (path.endsWith("/") || path.endsWith("\\")) {
 | 
	
		
			
				|  |  | +                path = path.substring(0, path.length() - 1);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            int lastSlashIndex = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'));
 | 
	
		
			
				|  |  | +            return lastSlashIndex >= 0 ? path.substring(lastSlashIndex + 1) : path;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 |