本文介绍了Apache Tika为CSV错误检测到mime类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用excel创建了.csv文件,并使用apache tika编写了以下代码:

I've created .csv file using excel and I wrote following code using apache tika:

public static boolean checkThatMimeTypeIsCsv(InputStream inputStream) throws IOException {
    BufferedInputStream bis = new BufferedInputStream(inputStream);
    AutoDetectParser parser = new AutoDetectParser();
    Detector detector = parser.getDetector();
    Metadata md = new Metadata();
    MediaType mediaType = detector.detect(bis, md);
    return "text/csv".equals(mediaType.toString());
}

public static void main(String[] args) throws IOException {
    System.out.println(checkThatMimeTypeIsCsv(new FileInputStream("Data.csv")));
}

但是它返回false'.

蒂卡很糟糕还是我错过了什么?

Does Tika so bad or I missed something?

推荐答案

尝试一下...

public static String checkThatMimeTypeIsCsv(String fileName ) throws Exception {
    File sourceFile = new File(fileName );
    DefaultDetector file_detector = new DefaultDetector();
    TikaInputStream file_stream = TikaInputStream.get(sourceFile);
    Metadata metadata = new Metadata();
    metadata.set(Metadata.RESOURCE_NAME_KEY, sourceFile.getName());
    MediaType mediaType = file_detector.detect(file_stream, metadata);
    String file_type = mediaType.toString();
    System.out.println(file_type);
    return file_type;
}

这篇关于Apache Tika为CSV错误检测到mime类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 08:03