Use cos in JSP to upload Web files (Modified Version)

Source: Internet
Author: User
Tags cos

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.