First of all I am using the com.oreilly.servlet package, which is
free to use, and I am perfectly aware that there are really a lot of
alternatives to that one.
There is a lot of development going on in this sectors
at the moment, and even here on java.apache.org the Turbine project is trying
to create a reusable framework, so check the projects page
http://java.apache.org/turbine.
There is also a note from Brian:
>This mailing list was started about a year and a half ago with this in
>mind. If you look in CVS you should see a template parser that I wrote. I'm
>not sure where it is; I haven't touched Java in about a year.
>..Brian
Maybe it would be great for the people if somebody would locate the above
mentioned template parser more exactly, so that everybody can find it.
So much about that. Now coming back to the file upload. I am going to
try to exercise through an example using the com.oreilly.servlet package
(which should be freely available from the net).
1. you need a page with a form, looking like already posted:
<form enctype="multipart/form-data" method="post" action="/meter/UploadFile">
Add File: <input type="file" name="newFile">
<input type="submit" value="Upload">
</form>
(There is no problem to extend this form...just a simple ex. taken from
this mailing-list)
2. you need the cos package in the classpath, and make sure it is accessible
for the JVM your servlet engine is running on.
3. import the right class files in your UploadFile servlet (all will ensure
you imported any class you really need).
import com.oreilly.servlet.*;
4. Now we take a look at the code of the UploadFile servlet:
/**
* I will override the service method, be aware that you can also
* override the doPost...but I prefer this.
*/
public void service(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException {
//Prepare for reception of a multipart request
MultipartRequest multi=null;
//Prepare a string describing the upload directory path, and an int
for describing
//maximum upload size in bytes.
//There are different approaches (const., properties etc.), I just
place it here for now.
String uploadDirectory=directoryName;
int maxFileSize= 1024 * 1024; //=1 MB, default set for cos anyway.
try {
multi = new MultipartRequest(req, uploadDirectory,maxFileSize);
} catch (IOException ioex) {
//you can handle the exception for upload size limits
or "real" io problems
//here.
}
//If you placed also other things in the form you can now retrieve
those simply by using:
//String myParam=multi.getParameter("<param name>");
//You can also create nice outputs or do something else with the file etc.
//Check the cos API to see what you can do with the MultipartRequest.
}
5. Thats it, pretty simple or :) I guess you know now why people are
using the package.
Allright here some links for those that want to find the Servlet
Programming book,
and a link to the rfc that actually defines the file uploads.
The book:
http://www.oreilly.com/catalog/jservlet
The RFC (although most probably it is recommended to use a mirror
close to your location)
http://ds.internic.net/rfc/rfc1867.txt
------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
Problems?: [EMAIL PROTECTED]