java.lang.NullPointerException while creating DiskFileItem

前端 未结 5 1629
我寻月下人不归
我寻月下人不归 2021-01-13 04:14

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 wo

5条回答
  •  死守一世寂寞
    2021-01-13 05:11

    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.

提交回复
热议问题