Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
771 views
in Technique[技术] by (71.8m points)

java - MIME-type checking with JMimeMagic - MagicMatchNotFoundException

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

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

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

Code:

    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;
    }
}

Output (only if it's "wrong" type):

 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)  

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)

Question:

  • How do solve this arises of exception?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Note that JMimeMagic has other 3rd party dependencies:

  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

Update - MagicMatchNotFoundException

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

Update 2 - OutOfMemoryError

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...