Recipe 8.2 Using the com.oreilly.servlet Library
You want to use the
com.oreilly.servlet classes that
O'Reilly author Jason Hunter has developed to handle
Of course, this isn't much of a problem, as
Jason's library takes most of the work out of
uploading and accepting files. I use Jason's library
here (with his permission, of course) because it handles file uploads
nicely, and there's no good reason to reinvent a
perfectly good wheel.
Download the distribution ZIP file from http://www.servlets.com/cos/index.html. Add
the cos.jar file, which is part of the
distribution to the WEB-INF/lib directory of
your web application. Make sure that you adhere to the software
license when using the library.
A JAR file named cos.jar includes the
com.oreilly.servlet.multipart packages. These
packages include several classes, such as all of the Java classes
that begin with "Multipart," which
can be used to handle file uploading in a servlet.
The cos.jar archive also contains many other
interesting and useful classes to use with servlets, but the
following recipes focus on file uploads.
Download the latest ZIP file containing the distribution from
contents of the ZIP file include
cos.jar, which you need to add to the
WEB-INF/lib directory of your web application.
In your servlet, you then import the classes that you want to use:
Before you have integrated these classes into your code, make sure
that you have read the accompanying software license for this code:
The rest of the recipes in this chapter assume that you have
cos.jar and the classes it contains available in
your web application. If you don't take steps to
make these classes available, none of the examples in this chapter
will function properly.
Recipe 8.1 on preparing the HTML for a file
upload; Recipe 8.5 on handling a single file
upload; Recipe 8.6 on handling multiple file
uploads in a servlet; Recipe 8.5 on controlling file naming; Recipe 8.6 on using a JSP to handle file uploads; the homepage for
com.oreilly.servlet: http://www.servlets.com/cos/index.html; the
RFC 1867 document on form-based file uploads: http://www.ietf.org/rfc/rfc1867.txt.