org.apache.commons.fileupload.disk.DiskFileItem is not created properly?

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

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?


The following does print the file content:

BufferedReader input =  new BufferedReader(new FileReader(TEST_FILE));
String line = null;
while (( line = input.readLine()) != null){


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.

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);
        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 =;
        while ( ret != -1 )
            ret =;
        System.out.println("diskFileItem.getString() = " + fileItem.getString());
    catch (Exception e)
        // TODO Auto-generated catch block

Source: stackoverflow