当前位置: 首页 > 知识库问答 >
问题:

检测多部分/签名

公西宏毅
2023-03-14

我正在使用Tika来自动检测被推入DMS的文档的内容类型。几乎所有的工作都很好,除了电子邮件。

Content-Type: multipart/signed; protocol="application/x-pkcs7-signature"; micalg=sha1; boundary="----4898E6D8BDE1929CA602BE94D115EF4C"
Detector detector;
List<Detector> detectors = new ArrayList<Detector>();
detectors.add(new ZipContainerDetector());
detectors.add(new POIFSContainerDetector());
detectors.add(MimeTypes.getDefaultMimeTypes());
detector = new CompositeDetector(detectors);
String mimetype = detector.detect(TikaInputStream.get(new File(args[0])), new Metadata()).toString();

我引用了核心库和tika解析器来检测pdf和msword文档。我是不是还漏掉了什么?

共有1个答案

淳于博文
2023-03-14

我解决了我的问题。我通过实现detector接口实现了一个自定义检测器:

public class MultipartSignedDetector implements Detector {

  @Override
  public MediaType detect(InputStream is, Metadata metadata) throws IOException {

    TemporaryResources tmp = new TemporaryResources();

    TikaInputStream tis = TikaInputStream.get(is, tmp);
    tis.mark(Integer.MAX_VALUE);

    try {

      MimeMessage mimeMessage = null;
      String host = "host.com";
      Properties properties = System.getProperties();
      properties.setProperty("mail.smtp.host", host);
      Session session = Session.getDefaultInstance(properties);

      mimeMessage = new MimeMessage(session, tis);

      if(mimeMessage.getContentType() != null && mimeMessage.getMessageID() != null && mimeMessage.getContentType().toLowerCase().contains("multipart/signed"))
        return new MediaType("multipart", "signed");
      else
        return MediaType.OCTET_STREAM;

    } catch(Exception e) {
      return MediaType.OCTET_STREAM;
    } finally {
      try {
        tis.reset();
        tmp.dispose();
      } catch (TikaException e) {
        // ignore
    }
  }
 }
}

然后在默认检测器之前将自定义检测器添加到复合检测器中:

Detector detector;
List<Detector> detectors = new ArrayList<Detector>();
detectors.add(new ZipContainerDetector());
detectors.add(new POIFSContainerDetector());

detectors.add(new MultipartSignedDetector());

detectors.add(MimeTypes.getDefaultMimeTypes());
detector = new CompositeDetector(detectors);
String mimetype = detector.detect(TikaInputStream.get(new File(args[0])), new Metadata()).toString();
 类似资料:
  • 我正在尝试测试我的上传我是usig Junit,Mockmvc和Spring 你能帮帮我吗? 错误堆栈跟踪: [org.springframework.test.context.support.dependencyinjectiontestexecutionlistener@361cb7a1]准备测试实例[endpoint.security.tests.uploadtest@6F7918F0]or

  • 我只是在创建一个检测二分图的算法,但我想到了一些我不确定算二分图的图,尽管我的算法说它算二分图。

  • 我试图执行一个对jaxrs服务的请求,该服务的媒体类型设置为。这个请求包含一个实体列表(xml)和一个映像(png,二进制)。我已经通过Balusc创建了这个线程中描述的请求。 在wireshark中检查后,请求似乎可以,除了ip头校验和错误之外。(表示“可能是由ip校验和卸载引起的”) 我这里最大的问题是如何在服务端处理多部分请求。我不希望包含Apache.cxf、resteasy或任何类似的库

  • 我正在使用Spring 5.0.1和servlet 3.1.0 但是当用户发送multipart/report(content-type)时,spring没有正确解析这个请求。 它没有给出任何例外情况,但是它没有在请求部分中存储任何内容。 request.getparts()将返回空数组。 是否需要进行任何配置,以便spring解析任何类型的多部分数据。 在下面张贴我的代码和请求负载: 与请求一起

  • 我一直得到以下错误 org.springframework.web.multipart.support.MissingServletRequestPartException:找不到请求部分“model”。 当向spring mvc控制器发送多部分请求时。 这是请求: 谢谢你,詹姆斯

  • 本教程上接教程第4部分。 我们已经建立一个网页投票应用,现在我们将为它创建一些自动化测试。 自动化测试简介 什么是自动化测试? 测试是检查你的代码是否正常运行的简单程序。 测试可以划分为不同的级别。 一些测试可能专注于小细节(某一个模型的方法是否会返回预期的值?), 其他的测试可能会检查软件的整体运行是否正常(用户在对网站进行了一系列的操作后,是否返回了正确的结果?)。这些其实和你早前在教程 1中