Class FileType

java.lang.Object
org.miaixz.bus.core.io.file.FileType

public class FileType extends Object
文件类型判断工具类

此工具根据文件的前几位bytes猜测文件类型,对于文本、zip判断不准确,对于视频、图片类型判断准确

需要注意的是,xlsx、docx等Office2007格式,全部识别为zip,因为新版采用了OpenXML格式,这些格式本质上是XML文件打包为zip

Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • FileType

      public FileType()
  • Method Details

    • putFileType

      public static String putFileType(String fileStreamHexHead, String extName)
      增加文件类型映射 如果已经存在将覆盖之前的映射
      Parameters:
      fileStreamHexHead - 文件流头部Hex信息
      extName - 文件扩展名
      Returns:
      之前已经存在的文件扩展名
    • removeFileType

      public static String removeFileType(String fileStreamHexHead)
      移除文件类型映射
      Parameters:
      fileStreamHexHead - 文件流头部Hex信息
      Returns:
      移除的文件扩展名
    • getType

      public static String getType(String fileStreamHexHead)
      根据文件流的头部信息获得文件类型
      Parameters:
      fileStreamHexHead - 文件流头部16进制字符串
      Returns:
      文件类型,未找到为null
    • getType

      public static String getType(InputStream in, int fileHeadSize) throws InternalException
      根据文件流的头部信息获得文件类型
      Parameters:
      in - 文件流
      fileHeadSize - 自定义读取文件头部的大小
      Returns:
      文件类型,未找到为null
      Throws:
      InternalException - IO异常
    • getType

      public static String getType(InputStream in, boolean isExact) throws InternalException
      根据文件流的头部信息获得文件类型 注意此方法会读取头部一些bytes,造成此流接下来读取时缺少部分bytes 因此如果想复用此流,流需支持InputStream.reset()方法。
      Parameters:
      in - InputStream
      isExact - 是否精确匹配,如果为false,使用前64个bytes匹配,如果为true,使用前8192bytes匹配
      Returns:
      类型,文件的扩展名,in为null或未找到为null
      Throws:
      InternalException - 读取流引起的异常
    • getType

      public static String getType(InputStream in) throws InternalException
      根据文件流的头部信息获得文件类型 注意此方法会读取头部64个bytes,造成此流接下来读取时缺少部分bytes 因此如果想复用此流,流需支持InputStream.reset()方法。
      Parameters:
      in - InputStream
      Returns:
      类型,文件的扩展名,未找到为null
      Throws:
      InternalException - 读取流引起的异常
    • getType

      public static String getType(InputStream in, String filename) throws InternalException
      根据文件流的头部信息获得文件类型 注意此方法会读取头部64个bytes,造成此流接下来读取时缺少部分bytes 因此如果想复用此流,流需支持InputStream.reset()方法。
           1、无法识别类型默认按照扩展名识别
           2、xls、doc、msi头信息无法区分,按照扩展名区分
           3、zip可能为docx、xlsx、pptx、jar、war、ofd头信息无法区分,按照扩展名区分
       
      Parameters:
      in - InputStream
      filename - 文件名
      Returns:
      类型,文件的扩展名,未找到为null
      Throws:
      InternalException - 读取流引起的异常
    • getType

      public static String getType(InputStream in, String filename, boolean isExact) throws InternalException
      根据文件流的头部信息获得文件类型 注意此方法会读取头部一些bytes,造成此流接下来读取时缺少部分bytes 因此如果想复用此流,流需支持InputStream.reset()方法。
           1、无法识别类型默认按照扩展名识别
           2、xls、doc、msi头信息无法区分,按照扩展名区分
           3、zip可能为docx、xlsx、pptx、jar、war、ofd头信息无法区分,按照扩展名区分
       
      Parameters:
      in - InputStream
      filename - 文件名
      isExact - 是否精确匹配,如果为false,使用前64个bytes匹配,如果为true,使用前8192bytes匹配
      Returns:
      类型,文件的扩展名,未找到为null
      Throws:
      InternalException - 读取流引起的异常
    • getType

      public static String getType(File file, boolean isExact) throws InternalException
      根据文件流的头部信息获得文件类型
           1、无法识别类型默认按照扩展名识别
           2、xls、doc、msi头信息无法区分,按照扩展名区分
           3、zip可能为jar、war头信息无法区分,按照扩展名区分
       
      Parameters:
      file - 文件 File
      isExact - 是否精确匹配,如果为false,使用前64个bytes匹配,如果为true,使用前8192bytes匹配
      Returns:
      类型,文件的扩展名,未找到为null
      Throws:
      InternalException - 读取文件引起的异常
    • getType

      public static String getType(File file) throws InternalException
      根据文件流的头部信息获得文件类型
           1、无法识别类型默认按照扩展名识别
           2、xls、doc、msi头信息无法区分,按照扩展名区分
           3、zip可能为jar、war头信息无法区分,按照扩展名区分
       
      Parameters:
      file - 文件 File
      Returns:
      类型,文件的扩展名,未找到为null
      Throws:
      InternalException - 读取文件引起的异常
    • getTypeByPath

      public static String getTypeByPath(String path, boolean isExact) throws InternalException
      通过路径获得文件类型
      Parameters:
      path - 路径,绝对路径或相对ClassPath的路径
      isExact - 是否精确匹配,如果为false,使用前64个bytes匹配,如果为true,使用前8192bytes匹配
      Returns:
      类型
      Throws:
      InternalException - 读取文件引起的异常
    • getTypeByPath

      public static String getTypeByPath(String path) throws InternalException
      通过路径获得文件类型
      Parameters:
      path - 路径,绝对路径或相对ClassPath的路径
      Returns:
      类型
      Throws:
      InternalException - 读取文件引起的异常