简体   繁体   English

使用Content-Type标头从eml文件中提取附件名称

[英]Extracting attachment name from eml file using Content-Type header

I'm using Tika-server to parse bunch of eml files. 我正在使用Tika-server解析一堆eml文件。 Extracting both content and metadata of emls and attachments works fine while using /rmeta endpoint. 使用/rmeta端点时,提取emls和附件的内容以及元数据都可以正常工作。

Problem occurs with proper attachment file name. 正确的附件文件名出现问题。 When attachment part in raw eml file has got a following structure: 当原始eml文件中的附件部分具有以下结构时:

Content-Type: application/pdf; name="filename_a.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="filename_a.pdf"

everything works fine: extracted filename path in metadata object (in api response) is: 一切工作正常:元数据对象中的提取文件名路径(在api响应中)为:

"X-TIKA:embedded_resource_path": "/filename_a.pdf"

However some of my emails have got malformed header structure (missing filename in Content-Disposition) ie: 但是我的一些电子邮件的标头结构格式错误(Content-Disposition中缺少文件名),即:

Content-Type: application/pdf; name="filename_a.pdf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;

Then after parsing the whole eml I obtain: 然后在解析整个eml之后,我得到:

"X-TIKA:embedded_resource_path": "/embedded-1"

I checked in Tika's source code that filename meta is defined in \\org\\apache\\tika\\parser\\RecursiveParserWrapper.class here: 我在Tika的源代码中检查了\\ org \\ apache \\ tika \\ parser \\ RecursiveParserWrapper.class中定义的文件名元:

 private String getResourceName(Metadata metadata, RecursiveParserWrapper.ParserState state) {
        String objectName = "";
        if (metadata.get("resourceName") != null) {
            objectName = metadata.get("resourceName");
        } else if (metadata.get("embeddedRelationshipId") != null) {
            objectName = metadata.get("embeddedRelationshipId");
        } else {
            objectName = "embedded-" + ++state.unknownCount;
        }

        objectName = FilenameUtils.getName(objectName);
        return objectName;
    }

I was trying to access somehow mentioned filename attribute by inspecting Content-Type key in metadata object but it's not there. 我试图通过检查元数据对象中的Content-Type键来以某种方式访问​​提到的filename属性,但是它不存在。 (I assume that Tika assess Content-type key not just by looking into proper header hence needed filename is absent) (我认为Tika不仅通过查看适当的标头来评估Content-type密钥,因此缺少所需的文件名)

Therefore my question (since I'm not able to figure it out) is there a way to modify Tika source code to force filename extraction from Content-Type header when proper filename attribute in Content-Disposition header is missing? 因此,我的问题(由于我无法弄清楚)是否有一种方法可以修改Tika源代码,以在缺少Content-Disposition标头中的正确文件名属性时从Content-Type标头中强制提取文件名?

Ok, so I managed on my own. 好吧,所以我自己做。 The workaround is preety simple and straightforward. 解决方法非常简单明了。

One has to extend one of the conditions in \\org\\apache\\tika\\parser\\mail\\MailContentHandler.class . 必须扩展\\ org \\ apache \\ tika \\ parser \\ mail \\ MailContentHandler.class中的条件之一 In line 129 we have: 在第129行,我们有:

if (contentDispositionFileName != null) {
   submd.set("resourceName", contentDispositionFileName);
}

By extending with additional else block: 通过扩展其他else块:

if (contentDispositionFileName != null) {
   submd.set("resourceName", contentDispositionFileName);
} else {
    Map<String, String> contentTypeParameters = ((MaximalBodyDescriptor)body).getContentTypeParameters();
    String contentTypeFilename = (String)contentTypeParameters.get("name");
    submd.set("resourceName", contentTypeFilename);
}

we enforce the handler to look for an additional filename property in content type parameters. 我们强制处理程序在内容类型参数中寻找其他文件名属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有没有办法从Java中的文件扩展名生成Content-Type标头? - is there a way to generate a Content-Type header from a file extension in Java? 缺少进度侦听器Content-Type标头的FormData文件上传 - FormData File Upload with Progress Listener Content-Type header missing 使用 HttpClient4 上传文件时设置 header “Content-Type”的问题 - Problem with setting header “Content-Type” in uploading file with HttpClient4 Apache Tika 无法使用文件内容检测 Content-Type - Apache Tika unable to detect Content-Type using file content 将内容类型“message/rfc822”的附件转换为 .msg 文件 - Convert an attachment of Content-type "message/rfc822" to .msg file 将带有附件的电子邮件中的内容类型从 text/plain 转换为 text/html - Convert content-type from text/plain to text/html in an email with attachment Spring-更改内容类型HTTP标头 - Spring - changing Content-type HTTP header 如何在使用RestTemplate(来自其他客户端)时为分段上传中的文件设置内容类型 - How to set content-type for the file in multipart upload when using RestTemplate (from a rest client) Spring是否更改Content-Type的标题? - Spring Changes Header for Content-Type? 如何使Content-Type标头为可选? - How to make Content-Type header optional?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM