4

I am trying to use the code shown in the following example:

java.lang.NullPointerException while creating DiskFileItem

My Test method contains the following code:

final File TEST_FILE = new File("C:/my_text.txt");
final DiskFileItem diskFileItem = new DiskFileItem("fileData", "text/plain", true, TEST_FILE.getName(), 100000000, TEST_FILE.getParentFile());
diskFileItem.getOutputStream();

System.out.println("diskFileItem.getString() = " + diskFileItem.getString());

The text file exists in this location but the last line in the above code does not output the file content.

Any idea why?

N.B.

The following does print the file content:

BufferedReader input =  new BufferedReader(new FileReader(TEST_FILE));
String line = null;
while (( line = input.readLine()) != null){
    System.out.println(line);
}
Community
  • 1
  • 1
rapt
  • 11,810
  • 35
  • 103
  • 145

2 Answers2

8

In your first code snip you use an OutputStream and it doesn't work. In the second part you use an InputStream (or whatever impl of this) and it works :) You might want to try with getInputStream() instead... OutputStream is to write bytes not reading.

http://commons.apache.org/fileupload/apidocs/org/apache/commons/fileupload/disk/DiskFileItem.html

try this one, it's simple and from scratch just to help :

final File TEST_FILE = new File("D:/my_text.txt");
    //final DiskFileItem diskFileItem = new DiskFileItem("fileData", "text/plain", true, TEST_FILE.getName(), 100000000, TEST_FILE);
    try
    {
        DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("fileData", "text/plain", true, TEST_FILE.getName());
        InputStream input =  new FileInputStream(TEST_FILE);
        OutputStream os = fileItem.getOutputStream();
        int ret = input.read();
        while ( ret != -1 )
        {
            os.write(ret);
            ret = input.read();
        }
        os.flush();
        System.out.println("diskFileItem.getString() = " + fileItem.getString());
    }
    catch (Exception e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
TecHunter
  • 6,091
  • 2
  • 30
  • 47
  • I think you missed what they said in the link I've mentioned above. They call `diskFileItem.getOutputStream();` just to bypass some bug. But the API does say that `diskFileItem.getString())` should output the file. Anyway even if I use `BufferedReader input = new BufferedReader(new InputStreamReader(diskFileItem.getInputStream()));` it still does not output the file. As far as I can see, this means that `diskFileItem` does not contain the file content. – rapt Jan 23 '12 at 22:19
  • Thank you - it's working! I have no idea why they tried to create the `DiskFileItem` through the constructor. But using the factory definitely makes more sense and actually works. – rapt Jan 24 '12 at 15:49
  • i guess it doesn't matter, it's just that you tried to read out a file you just created but which is still empty. You have to write the file first then you can read it. Keep in mind that it's an object used to store files from request (like uploads) and not to manipulate local files. – TecHunter Jan 25 '12 at 14:06
2

A condensed solution with apache IOUtils :

final File TEST_FILE = new File("C:/my_text.txt");
final DiskFileItem diskFileItem = new DiskFileItem("fileData", "text/plain", true, TEST_FILE.getName(), 100000000, TEST_FILE.getParentFile());

InputStream input =  new FileInputStream(TEST_FILE);
OutputStream os = diskFileItem.getOutputStream();
IOUtils.copy(input, os);

System.out.println("diskFileItem.getString() = " + diskFileItem.getString());
Thomas
  • 1,008
  • 3
  • 16
  • 34