FileCleanerCleanup question

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

FileCleanerCleanup question

Davis Ford
Hi, it is my understanding from the commons-io javadoc, that if you create a
FileCleanerCleanup it will remove files in a directory once the reference to
the File is GC'd.

Given that, I'm using commons-fileupload, and I set the listener in web.xml
as:

<listener>

<listener-class>org.apache.commons.fileupload.servlet.FileCleanerCleanup</listener-class>
</listener>

In my servlet, I have this:

private static FileCleaningTracker tracker;

    @Override
    protected void initServletContext(ServletContext servletContext) {
        super.initServletContext(servletContext);
        tracker = FileCleanerCleanup.getFileCleaningTracker(servletContext);
    }

...and in my method that accepts a file upload, I add the
FileCleanerCleanup:

final DiskFileItemFactory factory = new DiskFileItemFactory(0, tempDir);
factory.setFileCleaningTracker(tracker);
final ServletFileUpload upload = new ServletFileUpload(factory);

My files are uploaded and stored in the temp directory as expected.  In the
processing of a file upload, I store the FileItem object in the session as
an attribute.  Now, if the session expires, I expect the attribute to be
GC'd and as a result, the FileCleanerCleanup would go and cleanup the
directory, but it does not.

Can anyone tell me what I may be doing wrong?

Thanks in advance,
Davis
Reply | Threaded
Open this post in threaded view
|

Re: FileCleanerCleanup question

Davis Ford
Hi, is anyone on this list familiar with commons-io / commons-fileupload?

If not, is there somewhere else I should be asking questions?

I still have the issue indicated below, and I have an additional question.

When you parse the request... =>

final List<FileItem> items = (List<FileItem>) upload.parseRequest(request);

commons-fileupload also generates files for form field items (e.g. text box
values).  This is an undesired side effect.  I want it to generate a file
for the actual file that is uploaded, but not the form fields.  Is there a
way to control that?

Thanks in advance,
Davis


On Mon, Oct 19, 2009 at 12:06 PM, Davis Ford
<[hidden email]>wrote:

> Hi, it is my understanding from the commons-io javadoc, that if you create
> a FileCleanerCleanup it will remove files in a directory once the reference
> to the File is GC'd.
>
> Given that, I'm using commons-fileupload, and I set the listener in web.xml
> as:
>
> <listener>
>
> <listener-class>org.apache.commons.fileupload.servlet.FileCleanerCleanup</listener-class>
> </listener>
>
> In my servlet, I have this:
>
> private static FileCleaningTracker tracker;
>
>     @Override
>     protected void initServletContext(ServletContext servletContext) {
>         super.initServletContext(servletContext);
>         tracker =
> FileCleanerCleanup.getFileCleaningTracker(servletContext);
>     }
>
> ...and in my method that accepts a file upload, I add the
> FileCleanerCleanup:
>
> final DiskFileItemFactory factory = new DiskFileItemFactory(0, tempDir);
> factory.setFileCleaningTracker(tracker);
> final ServletFileUpload upload = new ServletFileUpload(factory);
>
> My files are uploaded and stored in the temp directory as expected.  In the
> processing of a file upload, I store the FileItem object in the session as
> an attribute.  Now, if the session expires, I expect the attribute to be
> GC'd and as a result, the FileCleanerCleanup would go and cleanup the
> directory, but it does not.
>
> Can anyone tell me what I may be doing wrong?
>
> Thanks in advance,
> Davis
>
>
>


--
Zeno Consulting, Inc.
home: http://www.zenoconsulting.biz
blog: http://zenoconsulting.wikidot.com
p: 248.894.4922
f: 313.884.2977
Reply | Threaded
Open this post in threaded view
|

Re: FileCleanerCleanup question

Anand Shankar-2
Hi

You can get both form fields as well as non form fields ( file fields) by
commons file upload and store where you want. I am giving a complete code
for file upload. Hope it will be helpful for you.


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.sql.*;
import java.util.*;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.servlet.*;
import org.apache.commons.fileupload.disk.*;


public class MyUpload extends HttpServlet
   {
       public void doPost(HttpServletRequest request,HttpServletResponse
response)
                    throws IOException,ServletException
             {
              response.setContentType("text/html");
              PrintWriter out = response.getWriter();

 if(ServletFileUpload.isMultipartContent(request))
                  {

                    FileItemFactory factory=new DiskFileItemFactory();
                    ServletFileUpload upload=new ServletFileUpload(factory);
                    List fileItem=upload.parseRequest(request);
                    Iterator itr= fileItem.iterator();

                      while(itr.hasNext())
                      {
                          FileItem fi=(FileItem)itr.next();
                          if(fi.isFormField())
                          {
                              out.print(fi.getFieldName() + fi.getString());

                          }
                          else
                          {


    String filename=fi.getName();
    InputStream is=fi.getInputStream();
        size=is.available();
        conttype=fi.getContentType();

// thus you can get file as a InputStream. Now, You can save it in database
or in a dir as you wish.

                              }


                  }

}
}




Thanks!

Anand Shankar








t<FileItem>) upload.parseRequest(request);

>
> commons-fileupload also generates files for form field items (e.g. text box
> values).  This is an undesired side effect.  I want it to generate a file
> for the actual file that is uploaded, but not the form fields.  Is there a
> way to control that?
>
> Thanks in advance,
> Davis
>
>
> On Mon, Oct 19, 2009 at 12:06 PM, Davis Ford
> <[hidden email]>wrote:
>
> > Hi, it is my understanding from the commons-io javadoc, that if you
> create
> > a FileCleanerCleanup it will remove files in a directory once the
> reference
> > to the File is GC'd.
> >
> > Given that, I'm using commons-fileupload, and I set the listener in
> web.xml
> > as:
> >
> > <listener>
> >
> >
> <listener-class>org.apache.commons.fileupload.servlet.FileCleanerCleanup</listener-class>
> > </listener>
> >
> > In my servlet, I have this:
> >
> > private static FileCleaningTracker tracker;
> >
> >     @Override
> >     protected void initServletContext(ServletContext servletContext) {
> >         super.initServletContext(servletContext);
> >         tracker =
> > FileCleanerCleanup.getFileCleaningTracker(servletContext);
> >     }
> >
> > ...and in my method that accepts a file upload, I add the
> > FileCleanerCleanup:
> >
> > final DiskFileItemFactory factory = new DiskFileItemFactory(0, tempDir);
> > factory.setFileCleaningTracker(tracker);
> > final ServletFileUpload upload = new ServletFileUpload(factory);
> >
> > My files are uploaded and stored in the temp directory as expected.  In
> the
> > processing of a file upload, I store the FileItem object in the session
> as
> > an attribute.  Now, if the session expires, I expect the attribute to be
> > GC'd and as a result, the FileCleanerCleanup would go and cleanup the
> > directory, but it does not.
> >
> > Can anyone tell me what I may be doing wrong?
> >
> > Thanks in advance,
> > Davis
> >
> >
> >
>
>
> --
> Zeno Consulting, Inc.
> home: http://www.zenoconsulting.biz
> blog: http://zenoconsulting.wikidot.com
> p: 248.894.4922
> f: 313.884.2977
>



--
Anand Shankar
Reply | Threaded
Open this post in threaded view
|

Re: FileCleanerCleanup question

Martin Cooper-3
In reply to this post by Davis Ford
On Tue, Oct 20, 2009 at 10:22 AM, Davis Ford
<[hidden email]> wrote:

> Hi, is anyone on this list familiar with commons-io / commons-fileupload?
>
> If not, is there somewhere else I should be asking questions?
>
> I still have the issue indicated below, and I have an additional question.
>
> When you parse the request... =>
>
> final List<FileItem> items = (List<FileItem>) upload.parseRequest(request);
>
> commons-fileupload also generates files for form field items (e.g. text box
> values).  This is an undesired side effect.  I want it to generate a file
> for the actual file that is uploaded, but not the form fields.  Is there a
> way to control that?

Whether a file is created depends on whether the size is over the
(customisable) threshold. Below the threshold, the item's content will
be kept in memory; above the threshold, it will be written to disk. If
that's not flexible enough for your needs, you can provide your own
FileItem class and your own factory to instantiate it.

--
Martin Cooper


>
> Thanks in advance,
> Davis
>
>
> On Mon, Oct 19, 2009 at 12:06 PM, Davis Ford
> <[hidden email]>wrote:
>
>> Hi, it is my understanding from the commons-io javadoc, that if you create
>> a FileCleanerCleanup it will remove files in a directory once the reference
>> to the File is GC'd.
>>
>> Given that, I'm using commons-fileupload, and I set the listener in web.xml
>> as:
>>
>> <listener>
>>
>> <listener-class>org.apache.commons.fileupload.servlet.FileCleanerCleanup</listener-class>
>> </listener>
>>
>> In my servlet, I have this:
>>
>> private static FileCleaningTracker tracker;
>>
>>     @Override
>>     protected void initServletContext(ServletContext servletContext) {
>>         super.initServletContext(servletContext);
>>         tracker =
>> FileCleanerCleanup.getFileCleaningTracker(servletContext);
>>     }
>>
>> ...and in my method that accepts a file upload, I add the
>> FileCleanerCleanup:
>>
>> final DiskFileItemFactory factory = new DiskFileItemFactory(0, tempDir);
>> factory.setFileCleaningTracker(tracker);
>> final ServletFileUpload upload = new ServletFileUpload(factory);
>>
>> My files are uploaded and stored in the temp directory as expected.  In the
>> processing of a file upload, I store the FileItem object in the session as
>> an attribute.  Now, if the session expires, I expect the attribute to be
>> GC'd and as a result, the FileCleanerCleanup would go and cleanup the
>> directory, but it does not.
>>
>> Can anyone tell me what I may be doing wrong?
>>
>> Thanks in advance,
>> Davis
>>
>>
>>
>
>
> --
> Zeno Consulting, Inc.
> home: http://www.zenoconsulting.biz
> blog: http://zenoconsulting.wikidot.com
> p: 248.894.4922
> f: 313.884.2977
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: FileCleanerCleanup question

Davis Ford
Hi Martin - I was aware that you could set this threshold in the
DiskFileItemFactory -- however, regardless of what I set the value to, it
seems that the form's textbox values would be translated into
DiskFileItem(s) which would be persisted to a temp file.

In the end, I just set the threshold to something very large so the factory
never writes to disk, and I manage everything myself.  I had three major
issues:

1) Stop persisting form fields
2) FileCleanerCleanup was not working to clean up files when a session
expired
3) File management was really flaky (e.g. some files would disappear as soon
as I uploaded a new file).

Writing my own code to persist the files and then setting the
FileCleanerCleanup#track on them specifically solves all my problems.

Regards,
Davis

On Wed, Oct 21, 2009 at 1:08 AM, Martin Cooper <[hidden email]> wrote:

> On Tue, Oct 20, 2009 at 10:22 AM, Davis Ford
> <[hidden email]> wrote:
> > Hi, is anyone on this list familiar with commons-io / commons-fileupload?
> >
> > If not, is there somewhere else I should be asking questions?
> >
> > I still have the issue indicated below, and I have an additional
> question.
> >
> > When you parse the request... =>
> >
> > final List<FileItem> items = (List<FileItem>)
> upload.parseRequest(request);
> >
> > commons-fileupload also generates files for form field items (e.g. text
> box
> > values).  This is an undesired side effect.  I want it to generate a file
> > for the actual file that is uploaded, but not the form fields.  Is there
> a
> > way to control that?
>
> Whether a file is created depends on whether the size is over the
> (customisable) threshold. Below the threshold, the item's content will
> be kept in memory; above the threshold, it will be written to disk. If
> that's not flexible enough for your needs, you can provide your own
> FileItem class and your own factory to instantiate it.
>
> --
> Martin Cooper
>
>
> >
> > Thanks in advance,
> > Davis
> >
> >
> > On Mon, Oct 19, 2009 at 12:06 PM, Davis Ford
> > <[hidden email]>wrote:
> >
> >> Hi, it is my understanding from the commons-io javadoc, that if you
> create
> >> a FileCleanerCleanup it will remove files in a directory once the
> reference
> >> to the File is GC'd.
> >>
> >> Given that, I'm using commons-fileupload, and I set the listener in
> web.xml
> >> as:
> >>
> >> <listener>
> >>
> >>
> <listener-class>org.apache.commons.fileupload.servlet.FileCleanerCleanup</listener-class>
> >> </listener>
> >>
> >> In my servlet, I have this:
> >>
> >> private static FileCleaningTracker tracker;
> >>
> >>     @Override
> >>     protected void initServletContext(ServletContext servletContext) {
> >>         super.initServletContext(servletContext);
> >>         tracker =
> >> FileCleanerCleanup.getFileCleaningTracker(servletContext);
> >>     }
> >>
> >> ...and in my method that accepts a file upload, I add the
> >> FileCleanerCleanup:
> >>
> >> final DiskFileItemFactory factory = new DiskFileItemFactory(0, tempDir);
> >> factory.setFileCleaningTracker(tracker);
> >> final ServletFileUpload upload = new ServletFileUpload(factory);
> >>
> >> My files are uploaded and stored in the temp directory as expected.  In
> the
> >> processing of a file upload, I store the FileItem object in the session
> as
> >> an attribute.  Now, if the session expires, I expect the attribute to be
> >> GC'd and as a result, the FileCleanerCleanup would go and cleanup the
> >> directory, but it does not.
> >>
> >> Can anyone tell me what I may be doing wrong?
> >>
> >> Thanks in advance,
> >> Davis
> >>
> >>
> >>
> >
> >
> > --
> > Zeno Consulting, Inc.
> > home: http://www.zenoconsulting.biz
> > blog: http://zenoconsulting.wikidot.com
> > p: 248.894.4922
> > f: 313.884.2977
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>


--
Zeno Consulting, Inc.
home: http://www.zenoconsulting.biz
blog: http://zenoconsulting.wikidot.com
p: 248.894.4922
f: 313.884.2977