资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java工具:通过文件头的魔数判断文件类型-创新互联

k-file-detector

github: https://github.com/kylin-hunter/k-file-detector

业务包括:企业网站建设、移动网站建设成都商城网站开发、网站营销推广等服务,并且涵盖申请域名雅安服务器托管、云服务器、等互联网基础服务;创新互联公司联建站以互联网的创新理念,成熟完善的建站体系,开拓进取的精神,专业的解决方案和顾问咨询服务,帮助客户在互联网时代提升形象、把握商机、实现价值,提高企业的核心竞争力。
介绍

一个检查文件类型的工具。

软件描述 根据文件的魔数 以及 内容判定文件类型
  1. 参考 https://www.garykessler.net/library/file_sigs.html
  2. 参考 xml/ooxml
安装教程 1、编译并发布到本地
gradle clean build publishToMavenLocal -x test
2、gradle中使用 (gradle.org)
implementation 'io.github.kylin-hunter:k-file-detector:1.0.11'
3、maven中使用 (maven.apache.org)
io.github.kylin-hunterk-file-detector1.0.11
使用说明
  1. 支持的文件类型:

https://github.com/kylin-hunter/k-file-detector/blob/main/k-file-detector/src/main/resources/signature/file_types.yml

  1. 通过file检测
DetectResult detectResult = FileDetector.detect(new File("xxxx.xxx"));  //通过file检测
        ListpossibleFileTypes = detectResult.getPossibleFileTypes();
        for (FileType fileType : possibleFileTypes) {System.out.println("id=" + fileType.getId()); // 文件类型唯一编号
            System.out.println("extensions=" + fileType.getExtensions()); // 文件类型对应的扩展名,可能是空
            System.out.println("desc=" + fileType.getDesc()); //文件类型描述
        }
  1. 通过文件流检测
try (InputStream in = new FileInputStream(new File("xxx.xxx"))) {DetectResult detectResult = FileDetector.detect(in); // 如果知道文件名也可以调用  FileDetector.detect(in,"xxx.xxx)
            ListpossibleFileTypes = detectResult.getPossibleFileTypes();
            for (FileType fileType : possibleFileTypes) {System.out.println("id=" + fileType.getId()); // 文件类型唯一编号
                System.out.println("extensions=" + fileType.getExtensions()); // 文件类型对应的扩展名,可能是空
                System.out.println("desc=" + fileType.getDesc()); //文件类型描述
            }
        }
  1. 通过字节检测
byte[] content = org.apache.commons.io.FileUtils.readFileToByteArray(new File("xxx.xxx"));
        DetectResult detectResult = FileDetector.detect(content); // 如果知道文件名也可以调用  FileDetector.detect(content,"xxx.xxx)
        ListpossibleFileTypes = detectResult.getPossibleFileTypes();
        for (FileType fileType : possibleFileTypes) {System.out.println("id=" + fileType.getId()); // 文件类型唯一编号
            System.out.println("extensions=" + fileType.getExtensions()); // 文件类型对应的扩展名,可能是空
            System.out.println("desc=" + fileType.getDesc()); //文件类型描述
        }
  1. 结果打印示例:
2022-12-02 22:46:13.541 [main] INFO   -  io.github.kylinhunter.Test[25]: /Users/kylinhunter/k-file-detector/k-file-detector/src/test/resources/files/detected/office/2007/docx.docx
2022-12-02 22:46:13.677 [main] INFO   -  io.github.kylinhunter.Test[29]: id=k_docx
2022-12-02 22:46:13.677 [main] INFO   -  io.github.kylinhunter.Test[30]: extensions=[docx]
2022-12-02 22:46:13.677 [main] INFO   -  io.github.kylinhunter.Test[31]: desc=Microsoft Office  Word  File

2022-12-02 22:46:13.973 [main] INFO   -  io.github.kylinhunter.Test[25]: /Users/kylinhunter/k-file-detector/k-file-detector/src/test/resources/files/detected/audio_video/mov.mov
2022-12-02 22:46:13.974 [main] INFO   -  io.github.kylinhunter.Test[29]: id=mov
2022-12-02 22:46:13.974 [main] INFO   -  io.github.kylinhunter.Test[30]: extensions=[mov]
2022-12-02 22:46:13.974 [main] INFO   -  io.github.kylinhunter.Test[31]: desc=QuickTime movie file

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


文章名称:java工具:通过文件头的魔数判断文件类型-创新互联
浏览地址:http://cdkjz.cn/article/epioh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220