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
文件打印为text / plain而不是文本/属性文件
我不确定出了什么问题,并且没有太多关于添加自定义mime或自定义tika现有解析器的信息。
答案 0 :(得分:0)
您的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>
我希望这会有所帮助。