其他分享
首页 > 其他分享> > 如何将新的mime类型添加到apache tika

如何将新的mime类型添加到apache tika

作者:互联网

这是我阅读mime类型的课程.我正在尝试添加一个新的mime类型(属性文件)并读取它.

这是我的类文件:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package check_mime;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.tika.Tika;
import org.apache.tika.mime.MimeTypes;


public class TikaFileTypeDetector {

    private final Tika tika = new Tika();

    public TikaFileTypeDetector() {
        super();
    }

    public String probeContentType(Path path) throws IOException {

        // Check contents first
        String fileContentDetect = tika.detect(path.toFile());
        if (!fileContentDetect.equals(MimeTypes.OCTET_STREAM)) {
            return fileContentDetect;
        }

        // Try file name only if content search was not successful
        String fileNameDetect = tika.detect(path.toString());
        if (!fileNameDetect.equals(MimeTypes.OCTET_STREAM)) {
            return fileNameDetect;
        }

        return null;
    }

    public static void main(String[] args) throws IOException {

        Tika tika = new Tika();

        if (args.length != 1) {
            printUsage();
            return;
        }
        Path path = Paths.get(args[0]);

        TikaFileTypeDetector detector = new TikaFileTypeDetector();

        String contentType = detector.probeContentType(path);

        System.out.println("File is of type - " + contentType);
    }

    public static void printUsage() {
        System.out.print("Usage: java -classpath ... "
                + TikaFileTypeDetector.class.getName()
                + " ");
    }
}

docs我创建了一个自定义xml:

 <?xml version="1.0" encoding="UTF-8"?>
 <mime-info>
   <mime-type type="text/properties">
          <glob pattern="*.properties"/>
   </mime-type>
 </mime-info>

现在我如何添加到我的程序并阅读它.我是否必须创建解析器?我被困在这里

解决方法:

这将在Apache Tika 5 minute parser instructions中介绍.要添加对Java .properties文件的支持,您应该首先创建一个名为custom-mimetypes.xml的文件,并使用以下内容填充它:

<?xml version="1.0" encoding="UTF-8"?>
<mime-info>
  <mime-type type="text/properties">
     <_comment>Java Properties</_comment>
     <glob pattern="*.properties"/>
     <sub-class-of type="text/plain"/>
   </mime-type>
</mime-info>

接下来,你需要把它放在Tika能找到它的地方,并且名字正确.它必须在类路径中存储为org / apache / tika / mime / custom-mimetypes.xml.最简单的方法是创建该目录结构,移动新文件,然后将根目录添加到类路径中.对于部署,您应该将其包装到jar中并将其放在类路径中

如果你小心的话,你可以使用Tika App检查你的mime类型文件已加载.将您的代码作为jar封装,运行如下:

java -classpath tika-app-1.10-SNAPSHOT.jar:my-custom-mimetypes.jar org.apache.tika.cli.TikaCLI --list-supported-types | grep text/properties

或者,如果您将它放在本地目录中,请尝试类似的操作

ls -l org/apache/tika/mime/custom-mimetypes.xml
# Check a file was found, with some content in it
java -classpath tika-app-1.10-SNAPSHOT.jar:. org.apache.tika.cli.TikaCLI --list-supported-types | grep text/properties

如果没有显示你的mime类型,那么你没有得到正确的路径或文件名,请仔细检查它们

(或者,升级到更新版本的Apache Tika,因为自r1686315 Tika内置了Java Properties mimetype!)

标签:java,apache-tika
来源: https://codeday.me/bug/20190708/1400190.html