对于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解析器来检索正确的mimetype,但它仍然给我带来了糟糕的mimetype……
发布于 2021-11-29 19:33:56
更新你的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。您可以阅读有关此here的更多信息。
但是,分析归档可能会导致性能下降。为了防止出现这种情况,您可以根据您的用例来确定mime类型(见下文),或者使用现有的mime类型,如Content-Type header。
final Metadata metadata = new Metadata();
metadata.add(TikaCoreProperties.RESOURCE_NAME_KEY, fileName);
detector.detect(stream, metadata);在超文本传输协议请求中,文件名也可能在Content-Disposition header中。
https://stackoverflow.com/questions/70158982
复制相似问题