You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Tika 2.6版本中正确检测Adobe Illustrator文件?

解决Tika 2.6无法正确检测Adobe Illustrator文件MIME类型的问题

问题原因

Adobe Illustrator(.ai)文件通常会嵌入PDF流,Tika默认检测逻辑会优先识别文件中的PDF内容,因此返回application/pdf而非预期的application/illustrator。

解决方案

1. 手动传入文件名辅助检测

Tika的检测器会结合文件内容与文件名进行类型判断,确保Metadata中包含带.ai后缀的文件名,可帮助检测器精准识别:

public MediaType detectMimeTypeFromContent(@NonNull File file) throws IOException {
    TikaConfig config = TikaConfig.getDefaultConfig();
    Detector detector = config.getDetector();
    Metadata metadata = new Metadata();
    // 手动设置文件名到Metadata,传递带后缀的文件名
    metadata.set(Metadata.RESOURCE_NAME_KEY, file.getName());
    try (TikaInputStream tikaStream = TikaInputStream.get(file, metadata)) {
        return detector.detect(tikaStream, metadata);
    }
}

2. 自定义Tika配置优先检测AI类型

若文件名辅助仍无效,可通过自定义配置让AI检测器优先于PDF检测器执行:
创建tika-config.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<properties>
    <detectors>
        <detector class="org.apache.tika.detect.image.IlliustratorDetector"/>
        <detector class="org.apache.tika.detect.DefaultDetector"/>
    </detectors>
</properties>

在代码中加载该配置:

// 替换为你的配置文件路径
TikaConfig config = new TikaConfig(new File("tika-config.xml"));
Detector detector = config.getDetector();
// 后续检测逻辑同前

3. 确认测试文件有效性

确保测试文件为原生Adobe Illustrator格式,而非仅包含PDF内容的导出文件,避免因文件本身结构问题导致检测偏差。

内容的提问来源于stack exchange,提问作者Ben

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 10:20:38