KeepChallenging
V2EX  ›  Java

Apache Tika 如何创建新的 Detector

  •  
  •   KeepChallenging · Aug 14, 2022 · 1322 views
    This topic created in 1368 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   ·   5997 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 02:21 · PVG 10:21 · LAX 19:21 · JFK 22:21
    ♥ Do have faith in what you're doing.