创建 DiskFileItem 时出现 java.lang.NullPointerException

发布于 2024-09-30 17:01:33 字数 3796 浏览 2 评论 0原文

我正在尝试编写一个单元测试来使用 Spring 3 处理文件上传控制器。现在,如果我通过控制器将图像发送到我的服务方法,则一切正常。但是,当进行直接单元测试时,我遇到了空指针异常。

当我手动实例化 DiskFilteItem 内的属性“dfos”时,它似乎为 null,但在从控制器检索 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);

我觉得我的设置中缺少一些愚蠢的东西。我的 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>

此代码抛出以下堆栈跟踪

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 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. 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.

    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.

    <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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

痴意少年 2024-10-07 17:01:33

我遇到了同样的问题。问题是 DiskFileItem.getSize()DiskFileItem.getOutputStream() 暂时耦合,因为内部字段在 getOutputStream 中初始化并使用在getSize中。

解决方案是

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();

在将 diskFileItem 传递给 CommonsMultipartFile 的构造函数之前执行此操作。

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.

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.

赏烟花じ飞满天 2024-10-07 17:01:33

该日志的这一部分说了什么?

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

但我认为问题可能是因为这个:

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

它看起来像是指向一个目录而不是一个文件,所以也许这就是为什么当您想要获取 < 的大小时您会收到 NullPointerException 的原因代码>磁盘文件项

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

站稳脚跟 2024-10-07 17:01:33

如果上面的解决方案都不适合您,就像我遇到的那样:)只需放弃 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()));

And if none of the solutions above work for you as it happened for me :) simply ditch the DiskFileItem and use the solution bellow:

  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()));
情独悲 2024-10-07 17:01:33

虽然不完全与问题相关。在将一些文件上传代码部署到 GAE 时,我遇到了同样的异常。我从这里修改了systempuntoout的代码在GAE上使用apache fileupload(它使用apache commons)然后工作得很好。

Although not entirely related to the question. I ran into the same exception when deploying some file-upload code to 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.

才能让你更想念 2024-10-07 17:01:33
new DiskFileItem(fieldName, contentType, isFormField, fileName, sizeThreshold, file);

结果为 null 值。查看 文档 以了解内容错误或者您可能传递了一些 null 作为参数

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

results in a null value. Look into the docs to see what's wrong or maybe you pass some null as parameters

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文