Hi there!

I'm quite sure I found a bug in the fileUpload module:

In one of my projects I need to upload a file to the server. I used the following code:

       if (ServletFileUpload.isMultipartContent(req)) {
           DiskFileItemFactory factory = new DiskFileItemFactory();
//            factory.setSizeThreshold(0);
           ServletFileUpload upload = new ServletFileUpload(factory);
           try {
//                 Thread.sleep(10);
               List items = upload.parseRequest(req);
               Iterator iter = items.iterator();
               while (iter.hasNext()) {
                   FileItem item = (FileItem) iter.next();
                   String fieldName = item.getFieldName();
                   if (item.isFormField()) {
                       // add logger for info about form input type="file"
                       // fields
                   } else {
                       File file = null;
                       String ref = null;
                       do {
                           ref = generateFileReferenceId();
file = new File(getPath() + "WEB-INF" + File.separator + "files" + File.separator + ref);
                       } while (file.exists());

                       item.write(file);
....

The file I used for testing had a size of 29 bytes. Directly after restarting the server the above code usually successfully stores the file on the server. But the second and all following trials usually failed. A file with this name is stored on the server but has a size of 0 bytes. Tests with much bigger files (~600 kb) were always succesful. Inserting the line "Thread.sleep(10);" (see above in the code) significantly increases the chance of correct behaviour and with 500 ms the error didn't occur anymore. Alternatively setting a breakpoint to the same line works too. The sleep-command or breakpoint has to be somewhere above the parseRequest-command to avoid the error. Debugging showed me that the code-behaviour on this level is completely identical. The only difference I recognized was that the DeferredOutputStream of the second item (the one containing my file - the first one is a formfield and seemed to be always correct) had a size of 0. Changing the threshold of the factory (see other commented line above) had no effect except that setting the limit to 0 seemed to cause the error with the small file constantly (with the bigger file still working fine). Setting it to other values smaller 29 (for example 10) had no effect.
A colleague using the same code and same file had no problems at all.
I'm running a Windows Vista.
I will use the above code with the sleep-workaround for at least a few days so I might be able to react on further questions and suggestions.


Best regards,

Nils

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to