为什么jmimemagic会抛出扩展文件sql的异常?

时间:2017-07-13 23:15:12

标签: java playframework mime-types

您好我使用Java框架1.4和我有一个UnitTest类测试一个使用jmimemagic验证文件的类但是我有一个问题来验证sql扩展文件引发异常MagicMatchNotFoundException

为什么jmimemagic会使用扩展文件sql抛出异常?

微量

net.sf.jmimemagic.MagicMatchNotFoundException
at net.sf.jmimemagic.Magic.getMagicMatch(Magic.java:368)
at net.sf.jmimemagic.Magic.getMagicMatch(Magic.java:240)
at sui.validateFileTest$Utils.validateFile(validateFileTest.java:41)
at sui.validateFileTest.validateFileFormatInvalidTest(validateFileTest.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at play.test.PlayJUnitRunner$StartPlay$2$1.evaluate(PlayJUnitRunner.java:128)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at play.test.PlayJUnitRunner.run(PlayJUnitRunner.java:70)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at org.junit.runner.JUnitCore.run(JUnitCore.java:105)
at org.junit.runner.JUnitCore.run(JUnitCore.java:94)
at play.test.TestEngine.run(TestEngine.java:188)
at controllers.TestRunner$1.doJobWithResult(TestRunner.java:101)
at controllers.TestRunner$1.doJobWithResult(TestRunner.java:1)
at play.jobs.Job$2.apply(Job.java:208)
at play.db.jpa.JPA.withTransaction(JPA.java:258)
at play.db.jpa.JPA.withinFilter(JPA.java:217)
at play.db.jpa.JPAPlugin$TransactionalFilter.withinFilter(JPAPlugin.java:298)
at play.jobs.Job.withinFilter(Job.java:185)
at play.jobs.Job.call(Job.java:204)
at play.jobs.Job$1.call(Job.java:119)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)

实用工具类

import java.io.File;
import java.util.Arrays;
import java.util.List;

public static class Utils {

    public static List<String> getFormats() {

        String[] formats = { "pdf", "doc", "docx", "csv", "xls", "xlsx", "odt", "jpg", "png", "jpeg" };

        List<String> list = Arrays.asList(formats);

        return list;

    }

    public static void validateFile(Validation validation, File file)
            throws MagicParseException, MagicMatchNotFoundException, MagicException {

        if (file == null)
            throw new IllegalArgumentException("File is null");

        Magic magic = new Magic();
        MagicMatch match = magic.getMagicMatch(file, false);

        if (file.length() == 0)
            validation.addError("file", "File is empty");

        if (!getFormats().contains(match.getExtension()))
            validation.addError("file", "Format not valid");

    }
}

测试类

import static org.hamcrest.CoreMatchers.is;
import java.io.File;
import org.junit.Before;
import org.junit.Test;
import net.sf.jmimemagic.Magic;
import net.sf.jmimemagic.MagicException;
import net.sf.jmimemagic.MagicMatch;
import net.sf.jmimemagic.MagicMatchNotFoundException;
import net.sf.jmimemagic.MagicParseException;
import play.data.validation.Validation;
import play.test.UnitTest;

public class validateFileTest extends UnitTest {

  private Validation validation;

  @Before
  public void init() {

    validation = Validation.current();
    validation.clear();
  }

  @Test
  public void validateFileFormatInvalidTest()
        throws MagicParseException, MagicMatchNotFoundException, MagicException {

    File file = new File("./docs/file-sql.sql");
    Utils.validateFile(validation, file);

    assertThat(validation.hasErrors(), is(true));
  }

  @Test
  public void validateFileFormatValidTest() throws MagicParseException, MagicMatchNotFoundException, MagicException {

    File file = new File("./docs/file-pdf.pdf");
    Utils.validateFile(validation, file);

    assertThat(validation.hasErrors(), is(false));

  }

  @Test(expected = IllegalArgumentException.class)
  public void validateFileNullTest() throws MagicParseException, MagicMatchNotFoundException, MagicException {

    Utils.validateFile(validation, null);

  }

  @Test
  public void validateFileEmptyTest() throws MagicParseException, MagicMatchNotFoundException, MagicException {

    File file = new File("./docs/file-empty.docx");
    Utils.validateFile(validation, file);

    assertThat(validation.hasErrors(), is(false));
  }
}

1 个答案:

答案 0 :(得分:0)

Jmimemagic不支持某些文件扩展名,因为它抛出此异常MagicMatchNotFoundException,要解决它我必须锁定一个多重方法“magic.getMagicMatch(file,false)”来捕获异常并将错误添加到字段中文件。

public static class Utils {

    public static List<String> getFormats() {

    String[] formats = { "pdf", "doc", "docx", "csv", "xls", "xlsx", "odt", "jpg", "png", "jpeg" };

    List<String> list = Arrays.asList(formats);

    return list;

   }

   public static void validateFile(Validation validation, File file) {

    if (file == null)
        throw new IllegalArgumentException("File is null");

    Magic magic = new Magic();
    MagicMatch match;
    try {

        match = magic.getMagicMatch(file, false);

        if (file.length() == 0)
            validation.addError("file", "File is empty");

        if (!getFormats().contains(match.getExtension()))
            validation.addError("file", "Format not valid");

    } catch (MagicParseException | MagicMatchNotFoundException | MagicException e) {
        validation.addError("file", "Format not valid");
    }

   }
}