简体   繁体   English

创建DiskFileItem时java.lang.NullPointerException

[英]java.lang.NullPointerException while creating DiskFileItem

I am trying to write a unit test for handling a file upload controller using Spring 3. Now if I send the image over to my service method through the controller everything works fine. 我正在尝试编写一个使用Spring 3处理文件上传控制器的单元测试。现在,如果我通过控制器将图像发送到我的服务方法中,则一切正常。 But when doing a straight unit tests I am getting a null pointer exception. 但是在进行直接单元测试时,我得到了空指针异常。

It appears that the property "dfos" inside the DiskFilteItem is null when I instantiate it manually but it is populated when retrieving a MultipartFile from the controller. 看来,财产“dfos”里面DiskFilteItem为空时,我手动实例,但检索时,它会填充MultipartFile从控制器。

    File file = new File("//Users//test//Downloads//testimage.jpg");
    log.info("found file: " +file.exists());
    log.info("file size: " +file.length());
    String fieldName = "field";
    String contentType = "image/jpeg";
    boolean isFormField = false;
    String fileName = "testimage.jpg";
    int sizeThreshold = 10240;

    DiskFileItemFactory factory = new DiskFileItemFactory();

    DiskFileItemFactory factory = new DiskFileItemFactory();
    // throws null pointer
    FileItem fi = factory.createItem(fieldName,contentType,isFormField,fileName);

    // so does this one
    DiskFileItem item = new DiskFileItem(fieldName, contentType, isFormField, fileName, sizeThreshold, file);
    MultipartFile f = new CommonsMultipartFile(item);

I feel like I am missing something silly in my setup. 我觉得我的装置中缺少一些愚蠢的东西。 My pom file contains the following dependencies. 我的pom文件包含以下依赖项。

    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2.2</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.0</version>
    </dependency>

This code throws the following stack trace 此代码引发以下堆栈跟踪

java.lang.NullPointerException
 at org.apache.commons.fileupload.disk.DiskFileItem.getSize(DiskFileItem.java:316)
 at org.springframework.web.multipart.commons.CommonsMultipartFile.(CommonsMultipartFile.java:60)
 at ImgurClientTest.testUploadImage(ImgurClientTest.java:58)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
 at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
 at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
 at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
 at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
 at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
 at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
 at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
 at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
 at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
 at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
 at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
 at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
 at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
 at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
 at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
 at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
 at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
 at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
 at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)

I ran into the same problem. 我遇到了同样的问题。 The issue is that DiskFileItem.getSize() is temporally coupled with DiskFileItem.getOutputStream() in that an internal field is initialized in getOutputStream and used in getSize . 问题在于DiskFileItem.getSize()在时间上与DiskFileItem.getOutputStream()耦合, DiskFileItem.getOutputStream()内部字段在getOutputStream初始化,并在getSize

The solution is to do 解决办法是

final File TEST_FILE = new File("src/test/resources/test.jpg");
final DiskFileItem diskFileItem = new DiskFileItem("file", "image/jpeg", true, TEST_FILE.getName(), 100000000, TEST_FILE.getParentFile());
diskFileItem.getOutputStream();

before passing diskFileItem to the constructor of CommonsMultipartFile . 在将diskFileItem传递给diskFileItem的构造函数CommonsMultipartFile

And if none of the solutions above work for you as it happened for me :) simply ditch the DiskFileItem and use the solution bellow: 如果上面的解决方案都不对您有用 ,就像我遇到的那样:)只需抛弃DiskFileItem并使用以下解决方案:

  final File fileToUpload = new File("src/test/resources/files/test.txt");
  final MultiValueMap<String, Object> request = new LinkedMultiValueMap<String, Object>();
  request.add("file", new FileSystemResource(fileToUpload.getAbsolutePath()));

What does this part of this log say? 此日志的这一部分怎么说?

log.info("found file: " +file.exists());
log.info("file size: " +file.length());

But I think the problem might be because of this: 但我认为问题可能是因为:

File file = new File("//Users//test//Downloads//");

It looks like it's pointing to a directory instead of a file, so maybe that is why you are getting a NullPointerException when you want to get the size of DiskFileItem 看起来它指向的是目录而不是文件,所以也许这就是为什么要获取DiskFileItem的大小时会出现NullPointerExceptionDiskFileItem

Although not entirely related to the question. 虽然与问题并不完全相关。 I ran into the same exception when deploying some file-upload code to GAE. 将某些文件上传代码部署到GAE时遇到了相同的异常。 I modified systempuntoout's code from here using apache fileupload on GAE (which uses the streams part of apache commons) which then worked fine. 我在这里使用GAE上的apache fileupload (使用apache commons的streams部分)修改了systempuntoout的代码,然后运行良好。

new DiskFileItem(fieldName, contentType, isFormField, fileName, sizeThreshold, file);

results in a null value. 结果为null值。 Look into the docs to see what's wrong or maybe you pass some null as parameters 查看文档 ,看看有什么问题,或者您可以传递一些null作为参数

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM