Recipe 8.2 Using the com.oreilly.servlet Library


Problem

You want to use the com.oreilly.servlet classes that O'Reilly author Jason Hunter has developed to handle file uploads.

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.


Solution

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.

Discussion

A JAR file named cos.jar includes the com.oreilly.servlet and 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 http://www.servlets.com/cos/index.html. The 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:

 import com.oreilly.servlet.MultipartRequest; import com.oreilly.servlet.multipart.FileRenamePolicy; 

Before you have integrated these classes into your code, make sure that you have read the accompanying software license for this code: http://www.servlets.com/cos/license.html.

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.


See Also

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.



Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net