Académique Documents
Professionnel Documents
Culture Documents
1 of 9
http://commons.apache.org/proper/commons-fileupload/using.html
Using FileUpload
FileUpload can be used in a number of different ways, depending upon the
requirements of your application. In the simplest case, you will call a single
method to parse the servlet request, and then process the list of items as
they apply to your application. At the other end of the scale, you might
decide to customize FileUpload to take full control of the way in which
individual items are stored; for example, you might decide to stream the
content into a database.
Here, we will describe the basic principles of FileUpload, and illustrate some
of the simpler - and most common - usage patterns. Customization of
FileUpload is described elsewhere.
FileUpload depends on Commons IO, so make sure you have the version
mentioned on the dependencies page in your classpath before continuing.
How it works
A file upload request comprises an ordered list of items that are encoded
according to RFC 1867, "Form-based File Upload in HTML". FileUpload can
parse such a request and provide your application with a list of the individual
uploaded items. Each such item implements the FileItem interface,
regardless of its underlying implementation.
This page describes the traditional API of the commons fileupload library.
The traditional API is a convenient approach. However, for ultimate
performance, you might prefer the faster Streaming API.
Each file item has a number of properties that might be of interest for your
application. For example, every item has a name and a content type, and
can provide an InputStream to access its data. On the other hand, you
may need to process items differently, depending upon whether the item is a
regular form field - that is, the data came from an ordinary text box or similar
HTML field - or an uploaded file. The FileItem interface provides the
03-Nov-2014 2:00 PM
2 of 9
http://commons.apache.org/proper/commons-fileupload/using.html
methods to make such a determination, and to access the data in the most
appropriate manner.
FileUpload creates new file items using a FileItemFactory . This is
what gives FileUpload most of its flexibility. The factory has ultimate control
over how each item is created. The factory implementation that currently
ships with FileUpload stores the item's data in memory or on disk, depending
on the size of the item (i.e. bytes of data). However, this behavior can be
customized to suit your application.
Now we are ready to parse the request into its constituent items.
03-Nov-2014 2:00 PM
3 of 9
http://commons.apache.org/proper/commons-fileupload/using.html
03-Nov-2014 2:00 PM
4 of 9
http://commons.apache.org/proper/commons-fileupload/using.html
Should you need further control over the parsing of the request, such as
storing the items elsewhere - for example, in a database - you will need to
look into customizing FileUpload.
For a regular form field, you will most likely be interested only in the name of
the item, and its String value. As you might expect, accessing these is
very simple.
03-Nov-2014 2:00 PM
5 of 9
http://commons.apache.org/proper/commons-fileupload/using.html
For a file upload, there are several different things you might want to know
before you process the content. Here is an example of some of the methods
you might be interested in.
With uploaded files, you generally will not want to access them via memory,
unless they are small, or unless you have no other alternative. Rather, you
will want to process the content as a stream, or write the entire file to its
ultimate location. FileUpload provides simple means of accomplishing both of
these.
03-Nov-2014 2:00 PM
6 of 9
http://commons.apache.org/proper/commons-fileupload/using.html
Resource cleanup
This section applies only, if you are using the DiskFileItem. In other words, it
applies, if your uploaded files are written to temporary files before
processing them.
Such temporary files are deleted automatically, if they are no longer used
(more precisely, if the corresponding instance of java.io.File is
garbage collected. This is done silently by the
org.apache.commons.io.FileCleaner class, which starts a reaper
thread.
This reaper thread should be stopped, if it is no longer needed. In a servlet
environment, this is done by using a special servlet context listener, called
FileCleanerCleanup. To do so, add a section like the following to your
web.xml :
Creating a DiskFileItemFactory
The FileCleanerCleanup provides an instance of
org.apache.commons.io.FileCleaningTracker . This instance
must be used when creating a
org.apache.commons.fileupload.disk.DiskFileItemFactory .
This should be done by calling a method like the following:
03-Nov-2014 2:00 PM
7 of 9
http://commons.apache.org/proper/commons-fileupload/using.html
03-Nov-2014 2:00 PM
8 of 9
http://commons.apache.org/proper/commons-fileupload/using.html
Watching progress
If you expect really large file uploads, then it would be nice to report to your
users, how much is already received. Even HTML pages allow to implement
a progress bar by returning a multipart/replace response, or something like
that.
Watching the upload progress may be done by supplying a progress listener:
Do yourself a favour and implement your first progress listener just like the
above, because it shows you a pitfall: The progress listener is called quite
frequently. Depending on the servlet engine and other environment factory, it
may be called for any network packet! In other words, your progress listener
may become a performance problem! A typical solution might be, to reduce
the progress listeners activity. For example, you might emit a message only,
if the number of megabytes has changed:
03-Nov-2014 2:00 PM
9 of 9
http://commons.apache.org/proper/commons-fileupload/using.html
What's next
Hopefully this page has provided you with a good idea of how to use
FileUpload in your own applications. For more detail on the methods
introduced here, as well as other available methods, you should refer to the
JavaDocs.
The usage described here should satisfy a large majority of file upload
needs. However, should you have more complex requirements, FileUpload
should still be able to help you, with it's flexible customization capabilities.
03-Nov-2014 2:00 PM