V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
KeepChallenging
V2EX  ›  Java

Apache Tika 如何创建新的 Detector

  •  
  •   KeepChallenging · Aug 14, 2022 · 1290 views
    This topic created in 1353 days ago, the information mentioned may be changed or developed.

    我创建了一个 Detector ,请问如何将这个 Detector 添加到 Tika ?

    package com.freedom.tika;
    
    import org.apache.commons.io.IOUtils;
    import org.apache.tika.detect.Detector;
    import org.apache.tika.metadata.Metadata;
    import org.apache.tika.mime.MediaType;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.nio.charset.StandardCharsets;
    
    /**
     * 自定义检测类型,目前主要为 svg
     */
    public class SvgDetector implements Detector {
    
        @Override
        public MediaType detect(InputStream input, Metadata metadata) throws IOException {
            String text = IOUtils.toString(input, StandardCharsets.UTF_8);
            if (text.startsWith("<svg") && text.endsWith("</svg>")) {
                return MediaType.parse("image/svg+xml");
            }
    
            return null;
    
        }
    }
    
    
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1025 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 18:55 · PVG 02:55 · LAX 11:55 · JFK 14:55
    ♥ Do have faith in what you're doing.