【问题标题】:Apache Tika - Incorrect MIME (content type) detectionApache Tika - 不正确的 MIME(内容类型)检测
【发布时间】:2015-12-24 13:39:26
【问题描述】:

我正在尝试将传递给 Web 服务的文件内容类型检测到 SOAP 信封中。 该文件可以通过两种方式表示:

  • 来自其网址,
  • 来自其包含(base64 压缩数据)。

此时,我可以将此文件转换为流缓冲区。 但是,我所有尝试获取其内容类型的尝试都失败了。 如果指示文件扩展名,则检测内容类型,否则内容始终检测为“纯文本”。

下面是我的课程代码:

类元数据分析器 {

private InputStream _is;

private File _file;

private void initializeAttributes() {

    _is = null;
    _file= null;

}


private void createTemporaryFile(byte[] pData) {

    FileOutputStream fos = null;
    try {
        _file = File.createTempFile(
                UUID.randomUUID().toString().replace("-", ""),
                null,
                new File("C:\\Users\\Florent\\Documents\\NetBeansProjects\\ServiceEdition\\tmp"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fos = new FileOutputStream(_file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        fos.write(pData);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    _file.deleteOnExit();

}

public MetadataAnalyser(byte[] pData) {

    initializeAttributes();
    _is = new ByteArrayInputStream(pData);
    createTemporaryFile(pData);

}

public MetadataAnalyser(InputStream pIs) {

    initializeAttributes();
    _is = pIs;
    _file = null;

}

public MetadataAnalyser(File pFile) {

    initializeAttributes();
    try {
        _file = pFile;
        _is = new FileInputStream(_file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

public MetadataAnalyser(String pFile) {

    initializeAttributes();
    try {
        _file = new File(pFile);
        if (_file.exists()) {
            _is = new FileInputStream(_file);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

public String getContentType() {

    AutoDetectParser parser = null;
    Metadata metadata = null;
    InputStream is = null;
    String mimeType = null;

    parser = new AutoDetectParser();
    parser.setParsers(new HashMap<MediaType, Parser>());
    metadata = new Metadata();
    if(_file != null) {
        metadata.add(TikaMetadataKeys.RESOURCE_NAME_KEY, _file.getName());
    }
    try {
        is = new FileInputStream(_file);
        parser.parse(is, new DefaultHandler(), metadata, new ParseContext());
        mimeType = metadata.get(HttpHeaders.CONTENT_TYPE);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (TikaException e) {
        e.printStackTrace();
    } finally {
        return mimeType;
    }

}

}

那么,即使文件扩展名未知,如何检测 MIME 类型?

【问题讨论】:

    标签: java apache-tika


    【解决方案1】:

    我不认为你可以检测到没有扩展名的 mime 类型,你需要知道哪个系统正在写入文件以及预计会有什么样的文件,并且基于你需要设置 MIME 类型(我猜你在回复中使用了它)。

    【讨论】:

      【解决方案2】:

      您需要确保在将内容发送到 Tika 之前对其进行解码,不,绝对不需要扩展,检测是通过此处描述的一个很好理解的 mime 魔术过程进行的:https://tika.apache.org/1.1/detection.html

      【讨论】:

        猜你喜欢
        • 2019-05-31
        • 2018-04-08
        • 1970-01-01
        • 2015-09-02
        • 2015-06-26
        • 2022-01-06
        • 2021-09-28
        • 2013-11-09
        • 2018-04-11
        相关资源
        最近更新 更多