【发布时间】:2019-01-30 16:15:03
【问题描述】:
这是我的相关代码sn-p:
for (Path path : Files.list(Paths.get(this.getClass().getClassLoader().getResource(directoryResource).getPath())).collect(Collectors.toList())) {
String mediaType = this.tikaService.getMimeType(Files.newInputStream(path));
assertEquals(Files.probeContentType(path), mediaType);
}
如您所知,this.tikaService.getMimeType(...) 会收到我使用 Files.newInputStream(path) 提供的 InputStream。
除了path 指向一个 zip 文件外,一切正常。
在这种情况下,Files.newInputStream() 指向的是 zip 文件的内容(嵌入文件),而不是指向 zip 文件。
有什么解决办法吗?
编辑
getMimeType代码:
public String getMimeType(InputStream is) {
TikaConfig tikaConfig = TikaConfig.getDefaultConfig();
Detector detector = tikaConfig.getDetector(); //new DefaultDetector();
Metadata metadata = new Metadata();
MediaType mediaType = detector.detect(TikaInputStream.get(is), metadata);
}
编辑 2
我还尝试使用此配置文件禁用ZipContainerDetector:
<?xml version="1.0" encoding="UTF-8"?>
<properties>
<detectors>
<!-- All detectors except built-in container ones -->
<detector class="org.apache.tika.detect.DefaultDetector">
<dhttps://stackoverflow.com/posts/52000097/editetector-exclude class="org.apache.tika.parser.pkg.ZipContainerDetector"/>
</detector>
</detectors>
</properties>
但结果是一样的。
【问题讨论】:
-
你能发布从
InputStream中找出 mediaType 的代码吗?将tikaService引用替换为实际代码。 -
我已添加
getMimeType代码。 -
你应该写下你的目标:可能你想知道 zip 中第一个文件的 mime 类型(如果我们可以假设每次调用这个函数,它都会得到一个具有相同结构的 zip 文件,在 zip 中只有文件,没有文件夹。如果我的假设是正确的,您应该使用 ZipInputStream 并且您必须在 zip 中找到所需的文件,并提供该文件到 TIKA。