使用JMimeMagic进行MIME类型检查 - MagicMatchNotFoundException [英] MIME-type checking with JMimeMagic - MagicMatchNotFoundException

查看:648
本文介绍了使用JMimeMagic进行MIME类型检查 - MagicMatchNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查MIME类型的 currentFile 。如果结果是成功的,并且文件具有MIME类型返回true 。如果没有检查成功返回false

有了这个目标,我使用 JMimeMagic

我尝试这样做根据这篇文章

I need check currentFile of MIME-type. If result is success and file have MIME-type return true. If wasn't checking succed return false.
With this goal I use JMimeMagic.
I try do this according this post

此代码的输出是 - net.sf.jmimemagic.MagicMatchNotFoundException

Output from this code is - net.sf.jmimemagic.MagicMatchNotFoundException

您需要使用 JDK 7 - 以这种方式将文件更改为字节[]( Files.readAllBytes (路径))。

You need have JDK 7 - for changing File to byte[] at this way(Files.readAllBytes(path)).

代码

    class ProbeContentTypeCheker implements Checker {

    @Override
    public boolean check(File currentFile) {
        String mimeType = null;
        try {
            Path path = Paths.get(currentFile.getAbsolutePath());
            byte[] data = Files.readAllBytes(path);
            MagicMatch match = Magic.getMagicMatch(data);
            mimeType = match.getMimeType();
        } catch (MagicParseException | MagicMatchNotFoundException
                | MagicException | IOException e) {
            e.printStackTrace();
        }

        if (null != mimeType) {
            return true;
        }

        return false;
    }
}

输出(只有这是错键入):

 net.sf.jmimemagic.MagicMatchNotFoundException
    at net.sf.jmimemagic.Magic.getMagicMatch(Magic.java:222)
    at net.sf.jmimemagic.Magic.getMagicMatch(Magic.java:170)
    at task.ProbeContentTypeCheker.check(FileScan.java:357)
    at task.FolderScan.findFiles(FileScan.java:223)
    at task.FolderScan.findFiles(FileScan.java:215)
    at task.FolderScan.run(FileScan.java:202)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)  

如果文件是oktype => output控制正常。但是经过一段时间后,另一个异常

If file is "ok" type => output to console normal. But after some time arise another exception:

Exception in thread "pool-1-thread-1" java.lang.OutOfMemoryError: Java heap space
    at java.lang.String.toCharArray(String.java:2753)
    at org.apache.oro.text.perl.Perl5Util.match(Unknown Source)
    at net.sf.jmimemagic.MagicMatcher.testRegex(MagicMatcher.java:663)
    at net.sf.jmimemagic.MagicMatcher.testInternal(MagicMatcher.java:433)
    at net.sf.jmimemagic.MagicMatcher.test(MagicMatcher.java:341)
    at net.sf.jmimemagic.Magic.getMagicMatch(Magic.java:208)
    at net.sf.jmimemagic.Magic.getMagicMatch(Magic.java:170)
    at task.ProbeContentTypeCheking.check(FileScan.java:384)
    at task.FolderScan.findFiles(FileScan.java:228)
    at task.FolderScan.findFiles(FileScan.java:225)
    at task.FolderScan.findFiles(FileScan.java:225)
    at task.FolderScan.run(FileScan.java:209)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

问题:


  • 如何解决这个异常?

推荐答案

JMimeMagic 0.1.2 依赖在Commons Logging 1.0.4
一个 NoClassDefFoundError 意味着Java虚拟机或ClassLoader实例尝试在类的定义中加载(作为普通方法调用的一部分,或作为使用新表达式创建新实例的一部分),并且不会找到类的定义。
解决方案是将commons-logging-1.0.4.jar添加到您的类路径中。

JMimeMagic 0.1.2 depends on Commons Logging 1.0.4 A NoClassDefFoundError means that the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found. The solution would be to add the commons-logging-1.0.4.jar to your classpath.

请注意,JMimeMagic还有其他第三方依赖关系

Note that JMimeMagic has other 3rd party dependencies:


  1. 雅加达ORO 2.0.8

  2. Log4j 1.2.8

  3. Xerces 2.4.0(可选)

  4. xml-apis 2.0.2

  5. xmlParserAPIs 2.0.2

  1. Jakarta ORO 2.0.8
  2. Log4j 1.2.8
  3. Xerces 2.4.0 (optional)
  4. xml-apis 2.0.2
  5. xmlParserAPIs 2.0.2

更新 - MagicMatchNotFoundException

如果没有找到mime类型匹配,则抛出一个href =http://jmimemagic.sourceforge.net/apidocs/net/sf/jmimemagic/MagicMatchNotFoundException.html =nofollow noreferrer> MagicMatchNotFoundException 数据。
您可以将日志级别 net.sf.jmimemagic 设置为DEBUG以获取有关正在发生的更多信息

The MagicMatchNotFoundException is thrown if no mime type match is found for the provided data. You can set the log level of net.sf.jmimemagic to DEBUG to get more information about what is going on

更新2 - OutOfMemoryError

OOM与JmimeMagic的行为有关。在某些情况下,它将尝试针对整个字节数组输入运行正则表达式,以找到魔术数字匹配。请参阅报告为Nuxeo企业平台报告的问题

I认为您可以通过限制您传递给的字节数组的大小来解决此问题getMagicMatch

The OOM looks related to the behavior of JmimeMagic. In some cases it will try to run a regular expression against the entire byte array input to find the magic number match. See this reported issue for the Nuxeo Enterprise Platform.
I think you can solve this issue by limiting the size of the byte array you pass to getMagicMatch

这篇关于使用JMimeMagic进行MIME类型检查 - MagicMatchNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆