估计文件是否为MIME类型 [英] Estimate if file is MIME type

查看:156
本文介绍了估计文件是否为MIME类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此方法应检查文件是否为MIME类型。为了实现这个目标,我使用方法 probeContentType()

但是,也许是另一种决定相同问题的方法。哪些是其他选择(如果有的话)?

This method should be check if a file is MIME type. To achieve this goal I am using method probeContentType().
However maybe is another way to decide same question. Which are the other alternatives (if there are any)?

代码:

class ProbeContentTypeCheker implements Checker {

    @Override
    public boolean check(File fileCheck) {
        try {
            Path filePath = FileSystems.getDefault().getPath(
                    fileCheck.getAbsolutePath());
            if ((Files.probeContentType(filePath) != null)) {
                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }
}

问题:


  • 还有其他方法可以检查文件中的MIME类型吗?

推荐答案

获取文件mime类型有两种方法:

There are 2 approaches for getting a file mime type:


  1. 通过文件的幻数 - 这是一种可靠的方法,但需要从文件中读取信息

  2. 通过文件扩展名来猜测 - 这是一种快速的方法,但可能不太准确

  1. Identifying by the file's magic number - this is a reliable approach but it requires reading information from the file
  2. Guessing it by the file extension - this is a fast approach, but can be less accurate

以下解决方案使用第一种方法:

The following solution use the 1st approach:


  1. Apache Tika - 使用现有解析器库从各种文档中检测和提取元数据和结构化文本内容的工具包

  2. JMimeMagic - 用于确定文件或流的MIME类型的Java库

  3. mime-util - 使Java程序能够根据文件扩展名,魔法数据和内容嗅探来检测MIME类型

  1. Apache Tika - a toolkit for detecting and extracting metadata and structured text content from various documents using existing parser libraries
  2. JMimeMagic - a Java library for determining the MIME type of files or streams
  3. mime-util - enable Java programs to detect MIME types based on file extensions, magic data and content sniffing

以下解决方案使用第二种方法:

The following solution use the 2nd approach:


  1. javax.activation.MimetypesFileTypeMap - JavaBeans Activation Framework的这一部分。 MimetypesFileTypeMap在用户系统的各个位置查找MIME类型文件条目。

  2. 使用java.net.URL - 扩展名和mime-type之间的映射在文件中定义[jre_home ] /lib/content-types.properties

有关更多信息,请参阅此发布

For some more information see this post

这篇关于估计文件是否为MIME类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆