Class BdUploadUtils

java.lang.Object
com.bytedesk.core.utils.BdUploadUtils

public class BdUploadUtils extends Object
文件上传相关工具类 包含文件扩展名提取、文件类型判断、文件名过滤等通用功能
Author:
jackning
  • Field Details

    • SAFE_FILENAME_PATTERN

      private static final Pattern SAFE_FILENAME_PATTERN
      安全文件名的正则表达式,只允许字母数字下划线点横线
  • Constructor Details

    • BdUploadUtils

      public BdUploadUtils()
  • Method Details

    • getFileExtension

      public static String getFileExtension(@Nullable String fileName)
      获取文件扩展名
      Parameters:
      fileName - 文件名
      Returns:
      文件扩展名(小写),如果没有扩展名返回空字符串
    • getFileNameWithoutExtension

      public static String getFileNameWithoutExtension(@Nullable String fileName)
      获取不带扩展名的文件名
      Parameters:
      fileName - 文件名
      Returns:
      不带扩展名的文件名
    • isImageFile

      public static boolean isImageFile(@Nullable String fileName, @Nullable String contentType)
      判断是否为图片文件
      Parameters:
      fileName - 文件名
      contentType - MIME类型
      Returns:
      是否为图片文件
    • isAudioFile

      public static boolean isAudioFile(@Nullable String fileName, @Nullable String contentType)
      判断是否为音频文件
      Parameters:
      fileName - 文件名
      contentType - MIME类型
      Returns:
      是否为音频文件
    • isVideoFile

      public static boolean isVideoFile(@Nullable String fileName, @Nullable String contentType)
      判断是否为视频文件
      Parameters:
      fileName - 文件名
      contentType - MIME类型
      Returns:
      是否为视频文件
    • isDocumentFile

      public static boolean isDocumentFile(@Nullable String fileName, @Nullable String contentType)
      判断是否为文档文件
      Parameters:
      fileName - 文件名
      contentType - MIME类型
      Returns:
      是否为文档文件
    • isArchiveFile

      public static boolean isArchiveFile(@Nullable String fileName, @Nullable String contentType)
      判断是否为压缩文件
      Parameters:
      fileName - 文件名
      contentType - MIME类型
      Returns:
      是否为压缩文件
    • getFileFolderByType

      public static String getFileFolderByType(@Nullable String fileName, @Nullable String contentType)
      根据文件类型获取对应的文件夹名称
      Parameters:
      fileName - 文件名
      contentType - MIME类型
      Returns:
      文件夹名称
    • sanitizeFileName

      public static String sanitizeFileName(@Nullable String fileName)
      过滤文件名,移除危险字符
      Parameters:
      fileName - 原始文件名
      Returns:
      安全的文件名
    • generateSafeFileName

      public static String generateSafeFileName(@Nullable String originalFileName)
      生成安全的文件名(时间戳 + 随机数 + 扩展名)
      Parameters:
      originalFileName - 原始文件名
      Returns:
      安全的文件名
    • truncateFileName

      public static String truncateFileName(@NonNull String fileName, int maxLength)
      截断文件名到指定长度,保留扩展名
      Parameters:
      fileName - 文件名
      maxLength - 最大长度
      Returns:
      截断后的文件名
    • extractRelativePathFromUrl

      public static String extractRelativePathFromUrl(@Nullable String fileUrl)
      从文件URL中提取相对路径 例如:http://127.0.0.1:9003/file/2025/09/05/filename.pdf 提取为:2025/09/05/filename.pdf
      Parameters:
      fileUrl - 完整的文件URL
      Returns:
      相对路径,如果提取失败返回null
    • extractDatePathFromTimestampFileName

      public static String extractDatePathFromTimestampFileName(@Nullable String fileName)
      从时间戳文件名中提取日期路径 例如:20240916144702_身份证-背面.jpg 提取为:2024/09/16
      Parameters:
      fileName - 包含时间戳的文件名
      Returns:
      日期路径,如果提取失败返回null
    • isSafeFileName

      public static boolean isSafeFileName(@Nullable String fileName)
      验证文件名是否安全
      Parameters:
      fileName - 文件名
      Returns:
      是否安全
    • formatFileSize

      public static String formatFileSize(long sizeInBytes)
      格式化文件大小
      Parameters:
      sizeInBytes - 文件大小(字节)
      Returns:
      格式化后的文件大小字符串
    • isExcelFile

      public static boolean isExcelFile(@Nullable String fileName)
      检查文件名是否为Excel文件
      Parameters:
      fileName - 文件名
      Returns:
      如果是Excel文件返回true,否则返回false
    • isWordDocument

      public static boolean isWordDocument(@Nullable String fileName)
      检查文件名是否为Word文档
      Parameters:
      fileName - 文件名
      Returns:
      如果是Word文档返回true,否则返回false
    • isPowerPointDocument

      public static boolean isPowerPointDocument(@Nullable String fileName)
      检查文件名是否为PowerPoint文档
      Parameters:
      fileName - 文件名
      Returns:
      如果是PowerPoint文档返回true,否则返回false