1. download the latest cos package (http://www.servlets.com/cos/cos-05Nov2002.zip), decompress the COs. Jar under the lib directory to add to classpath.
2. Compile a JSP file to be uploaded. For convenience, you can use a simple HTM file. on this page, you can upload three files at a time.
/// // Upload.htm ////////////////// //////////////////
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en"
Http://www.w3.org/TR/html4/loose.dtd>
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> upload a file </title>
</Head>
<Body>
<! -- The value of enctype is very important. Upload. jsp is the JSP for processing uploads -->
<Form name = "form1" method = "Post" enctype = "multipart/form-Data"
Action = "Upload. jsp">
<P>
<Input name = "file1" type = "file">
</P>
<P>
<Input name = "file2" type = "file">
</P>
<P> <input name = "file3" type = "file">
</P>
<P>
<Input type = "Submit" name = "Submit" value = "Upload">
</P>
</Form>
</Body>
</Html>
Create a directory under D:/tools/to store uploaded files.
3. Write a JSP or servlet for upload. I use Upload. jsp here, so you don't need to configure web. xml.
/// // Upload. JSP ////////////////////////
<% @ Page import = "Java. Io. *" %>
[Color = Red] <% @ page import = "Java. util. *" %> [/color]
<% @ Page import = "com. oreilly. servlet. multipartrequest" %>
<% @ Page import = "com. oreilly. servlet. multipart. [color = Red] default [/color] filerenamepolicy" %>
<% @ Page contenttype = "text/html; charset = gb2312" %>
<%
// After the file is uploaded, save it in D: // tools // upload
String savedirectory = "D: // tools // upload ";
// Each file can contain a maximum of 5 MB and a maximum of 3 files, so...
Int maxpostsize = 3*5*1024*1024;
// The response is encoded as "gb2312", and the default file name conflict resolution policy is used for uploading.
Multipartrequest multi =
New multipartrequest (request, savedirectory, maxpostsize,
"Gb2312 ");
// Output Feedback
Enumeration files = multi. getfilenames ();
While (files. hasmoreelements ()){
System. Err. println ("CCC ");
String name = (string) files. nextelement ();
File F = multi. GetFile (name );
If (F! = NULL ){
String filename = multi. getfilesystemname (name );
String lastfilename = savedirectory + "//" + filename;
Out. println ("uploaded file:" + lastfilename );
Out. println ("<HR> ");
}
}
%>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
4. Finally, just release the two files to your server.