使用 docx 文件,我检索
application/x-tika-ooxml
,但我应该检索 application/vnd.openxmlformats-officedocument.wordprocessingml.document
这是我的方法:
public String retrieveMimeType(InputStream stream) throws IOException, TikaException {
TikaInputStream tikaStream = null;
TikaConfig tikaConfig = new TikaConfig();
MediaType mediaType = null;
try {
mediaType = tikaConfig.getDetector().detect(TikaInputStream.get(stream), new Metadata());
} catch (Throwable t) {
throw t;
} finally {
if (tikaStream != null) {
try {
tikaStream.close();
} catch (IOException e) {
}
}
}
return mediaType.toString();
}
还有我的家属:
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers</artifactId>
<version>1.27</version>
</dependency>
我使用 tika-core 和 tika-parsers 来检索正确的 mimetype,但它仍然给我错误的 mimetype...
更新您的 tika 模块。
tika-core
及其模块的版本应始终相同。
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers-standard-package</artifactId>
<version>2.1.0</version>
</dependency>
新的微软文档格式(
docx
,xlsx
,...)只是来自外部的zip
档案。较旧的 tika 版本默认情况下不会查看它们,这就是为什么根据版本,它们会将它们检测为 application/zip
或 application/x-tika-ooxml
。您可以在此处阅读更多相关信息。
但是分析档案可能导致性能下降。为了防止这种情况,您可以根据您的用例,按名称确定 mime 类型(见下文)或使用现有的 mime 类型,例如 Content-Type 标头。
final Metadata metadata = new Metadata();
metadata.add(TikaCoreProperties.RESOURCE_NAME_KEY, fileName);
detector.detect(stream, metadata);
在 HTTP 请求中,文件名也可能位于 Content-Disposition 标头中。
对我来说,我会使用这些旧的 tika 版本,不知道为什么,但这能够得到我们想要的结果。
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>1.23</version>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-parsers</artifactId>
<version>1.23</version>
</dependency>