【问题标题】:Adding to custom detector class to apache tika将自定义检测器类添加到 apache tika
【发布时间】:2015-06-18 10:14:17
【问题描述】:
public class CustomDetector implements Detector {

public MediaType detect(InputStream stream, Metadata metadata) throws IOException {
    MediaType type = MediaType.OCTET_STREAM;

    InputStream lookahead = new LookaheadInputStream(stream, 1024);
    try {
        //Detect File Type
        File file = new File("ToolConfig.properties");
        Tika tika = new Tika();
        String filetype = tika.detect(file);

        //Read File content
        Properties properties = new Properties();
        properties.load(new FileInputStream("ToolConfig.properties"));
        for (String key : properties.stringPropertyNames()) {
            String value = properties.getProperty(key);
            if (key instanceof String && value instanceof String && filetype.contains("text/plain")) {
                type = MediaType.application("properties");
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        lookahead.close();
    }
    return type;
    }
}

使用 tika 我想根据属性文件中存在的键和值格式将 .properties 文件检测为properties file(text/properties),否则为text file(text/plain)

上面我写了一个自定义类,它实现了tika的Detector接口,还为mime类型创建了一个自定义文件:

<mime-info>
<mime-type type="text/properties">
<glob pattern="*.properties"/>
</mime-type>
</mime-info>

将上述自定义类与META-INF/services/org.apache.tika.detect.Detector file 一起添加到jar 文件中,但是当我运行程序时,它会将.properties 文件打印为文本/纯文本而不是文本/属性文件

我不确定出了什么问题,也没有太多关于添加自定义 mime 或自定义现有 tika 解析器的信息。

【问题讨论】:

  • Tika 看到你的探测器了吗?如果您向 DefaultDetector 询问子检测器,它会看到您的吗?您是否在服务文件中正确列出了您的班级名称?
  • 我不知道如何检查它是否被检测到。能否请你帮忙。我是新手
  • 只需在您从 TikaConfig 获得的 DefaultDetector 上调用 getDetectors(),它就会告诉您!
  • 没有方法叫getDetectors() 我只能看到getDetector()。我尝试从compositeDetector 扩展我的类并调用getDetectors() 方法返回空数组
  • 11.2.3 Plugging in new detectors - From the book TIka in Action Chris.A 在编译此自定义检测器类后,您需要做的最后一件事是将其插入 Tika。最简单的方法是将编译后的类与 META-INF/services/org.apache.tika.detect.Detector 文件一起放入 JAR 存档中,该文件在一行中包含此类的完全限定名称。然后将该 JAR 包含在您的类路径中,Tika 将自动拾取并使用新的检测器......我试过这个,可能是我没有正确构建 jar 文件

标签: java apache-tika


【解决方案1】:

您的 XML 似乎以(一些)空格开头,请尝试删除 XML 最开始处的空格,如下所示:

<mime-info>
    <mime-type type="text/properties">
        <glob pattern="*.properties"/>
    </mime-type>
</mime-info>

我希望您在文件的第一行添加一个 XML 声明,然后在下一行继续执行我上面提到的指令,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<mime-info>
    <mime-type type="text/properties">
        <glob pattern="*.properties"/>
    </mime-type>
</mime-info>

我希望这会有所帮助。

【讨论】:

  • 现在它的工作,但自定义类没有被调用。我的意思是一个属性应该显示为text/properties,但它显示为text/plain
  • 这是另一个需要不同 SO 问题的问题。我不熟悉 Apache Tika。我很高兴我的解决方案有所帮助。 :-)
  • 是的,确实有帮助,谢谢
  • 此外,您正在从ToolConfig.Properties 读取键/值。因此,请检查该文件中返回的键和值。
  • DB_NAME Africa5 DB_USERNAME db2inst1 DB_TYPE db2 SCHEMA SELFCARE_TEST PORT 50002 DB_PASSWORD 32A74934D1 4 IP 192.168.6.97
猜你喜欢
  • 2015-06-26
  • 2019-05-31
  • 2018-04-08
  • 2011-09-07
  • 2013-10-15
  • 1970-01-01
  • 1970-01-01
  • 2015-03-18
  • 1970-01-01
相关资源
最近更新 更多