commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Martin Cooper <>
Subject Re: Customize FileUpload to store files directly into database
Date Wed, 22 Oct 2003 03:33:48 GMT
On Tue, 21 Oct 2003 wrote:

> Hi,
> I want to store uploaded files directly into database instead of keeping
> them in memory or storing them temporarlily to disk. The documentation
> ( says that to get
> this done, I have to customize FileUpload. However, the description of how
> to do this customization is not available yet - the page says: "TODO:
> Document usage of factories and subclassing for customization"
> (
> Has anyone tried this yet and/or knows how to customize FileUpload
> properly?

I've done this as part of a Struts app. I don't have the code any more,
since I've changed employer since then, but I'll try to give you a rough
idea of what I had to do.

What you'll need to do is create your own FileItem implementation, along
with a factory to create those. Your getOutputStream() implementation is
where the work happens. There, you'll probably want to make a decision on
what kind of stream to return, based on whether or not the field is a form
field or a file upload.

For regular form fields, I used a ByteArrayOutputStream, since those are
generally fairly short. For file upload fields, I returned a custom stream
implementation that fed the data straight to the database.

In the latter case, you're going to need some context to obtain your
connection or EJB or whatever. This is where the factory comes in. The
trick is that you can provide the factory with your context (e.g. the
request object) and have it pass that to each FileItem it creates.

One thing to watch out for is that there is no notification mechanism to
allow you to find out that a particular file upload item has finished
uploading, other than the close() method on the output stream you return.
If you need to release a connection as soon as a particular item has been
parsed, then you'll need to do that in your close() method. Otherwise, you
can do cleanup after parseRequest() completes.

That's about all I can recall off the top of my head, but hopefully it's
enough to get you started.

> When will the documentation on the Apache FileUpload customizing page be
> available?

As soon as I find some time to write it down, with some code fragments,
and get it on the web site. ;-) Having just changed jobs recently, I'm a
little busy right now...

Martin Cooper

> Thank you!
> Steffi
> ____________________________________________________
> Internetauftritt der Deutschen Bahn AG >>
> ---------
> Diese E-Mail könnte  vertrauliche und/oder rechtlich geschützte
> Informationen enthalten. Wenn Sie nicht der richtige Adressat sind oder
> diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den
> Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die
> unbefugte Weitergabe dieser Mail sind nicht gestattet.
> This e-mail may contain confidential and/or privileged information. If you
> are not the intended recipient (or have received this e-mail in error)
> please notify the sender immediately and destroy this e-mail. Any
> unauthorised copying, disclosure or distribution of the material in this
> e-mail is strictly forbidden.
> ----------
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message