【发布时间】: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