簡體   English   中英

Apache Tika-不正確的MIME(內容類型)檢測

[英]Apache Tika - Incorrect MIME (content type) detection

我試圖檢測傳遞給SOAP信封的Web服務的文件內容類型。 該文件可用兩種方式表示:

  • 從其網址,
  • 從其包含(base64壓縮數據)。

至此,我可以將此文件轉換為流緩沖區。 但是,我所有嘗試獲取其內容類型的嘗試都失敗了。 如果指示了文件擴展名,則檢測到內容類型,否則始終將內容檢測為“純文本/文本”。

貝婁是我的課堂代碼:

類MetadataAnalyser {

private InputStream _is;

private File _file;

private void initializeAttributes() {

    _is = null;
    _file= null;

}


private void createTemporaryFile(byte[] pData) {

    FileOutputStream fos = null;
    try {
        _file = File.createTempFile(
                UUID.randomUUID().toString().replace("-", ""),
                null,
                new File("C:\\Users\\Florent\\Documents\\NetBeansProjects\\ServiceEdition\\tmp"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fos = new FileOutputStream(_file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        fos.write(pData);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    _file.deleteOnExit();

}

public MetadataAnalyser(byte[] pData) {

    initializeAttributes();
    _is = new ByteArrayInputStream(pData);
    createTemporaryFile(pData);

}

public MetadataAnalyser(InputStream pIs) {

    initializeAttributes();
    _is = pIs;
    _file = null;

}

public MetadataAnalyser(File pFile) {

    initializeAttributes();
    try {
        _file = pFile;
        _is = new FileInputStream(_file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

public MetadataAnalyser(String pFile) {

    initializeAttributes();
    try {
        _file = new File(pFile);
        if (_file.exists()) {
            _is = new FileInputStream(_file);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

public String getContentType() {

    AutoDetectParser parser = null;
    Metadata metadata = null;
    InputStream is = null;
    String mimeType = null;

    parser = new AutoDetectParser();
    parser.setParsers(new HashMap<MediaType, Parser>());
    metadata = new Metadata();
    if(_file != null) {
        metadata.add(TikaMetadataKeys.RESOURCE_NAME_KEY, _file.getName());
    }
    try {
        is = new FileInputStream(_file);
        parser.parse(is, new DefaultHandler(), metadata, new ParseContext());
        mimeType = metadata.get(HttpHeaders.CONTENT_TYPE);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (TikaException e) {
        e.printStackTrace();
    } finally {
        return mimeType;
    }

}

}

因此,即使文件擴展名未知,如何檢測MIME類型?

我認為您無法檢測到沒有擴展名的mime類型,您需要知道是哪個系統正在寫入文件,以及預期將存在哪種文件,並因此需要設置MIME類型(我想您在您的回復中使用它)。

您需要確保內容在發送到Tika之前已解碼,並且不需要,絕對不需要擴展,檢測是通過此處所述的眾所周知的mime魔術過程進行的: https//tika.apache.org/1.1/detection .html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM