JIRA [email protected] wrote:
> 
> 
>      [
> https://issues.apache.org/jira/browse/FILEUPLOAD-163?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
> ]
> 
>> This problem happens often but not always, sometimes it can be
>> successfully run.
>> The problem is when I attempt to reference the file object, the real data
>> file stored in my temporary directory soon got deleted before I can
>> reference. After some research on my own, I discovered that there could
>> be a file cleaning tracker there to clean up the temporary file, but
>> after disabling it it still didn't solve the bug.
> 
> 

I think I know what this guy was running into because I ran into the same
thing.  If you take a look at the documentation at:

 http://commons.apache.org/fileupload/using.html

and you look at the section titled "Creating a DiskFileItemFactory", you'll
see how there is a nice little *static* method for creating a
DiskFileItemFactory.

Being the lazy programmer I am, I just copied and pasted this.  

In my particular case, I am using ajax to upload files one at a time and I'm
keeping a set of FileItem objects in the user's session.

However, I was calling this nice little static method *in each request*,
thus creating a new instance of the DiskFileItemFactory each time.

What I found was, that even though my DiskFileItem instances wouldn't get
lost (because they were stored in the session) the temp file was getting
deleted every time GC would run.

I guess even though the File object wasn't GC'd, when the
DiskFileItemFactory was GC'd it would delete the temp files.  This kind of
contradicts the API docs in my opinion, but that is beside the point.

Anyhow, once I changed my servlet so that the DiskFileItemFactory was
instantiated only once in the init method of my servlet, now everything
works as it should.

My suggestion would be to take out that little static method example and
instead show the example in the context of a servlet or portlet init method
instead.

Maybe that would prevent other lazy programmers like myself from falling in
this trap. :)

-- 
View this message in context: 
http://www.nabble.com/-jira--Created%3A-%28FILEUPLOAD-163%29-Temporary-file-soon-got-deleted-before-I-can-reference-tp18555213p21927236.html
Sent from the Commons - Issues mailing list archive at Nabble.com.

Reply via email to