Use cos components to upload attachments in jsp and jsp to upload attachments

Source: Internet
Author: User

Use cos components to upload attachments in jsp and jsp to upload attachments

Requirement: The attachment upload function is one of the most basic functions in the web function. Therefore, a demo of attachment upload is provided using the cos component. The attachment upload function can be implemented using other java components.

 

Instructions: Download components and install --> code implementation --> test the attachment upload function

 

Download the cos component and install it to the web Project

1. Access http://www.servlets.com/

2. ClickDownload the packageHyperlink

3. ClickCos-26Dec2008.zipDownload Components

4. decompress the downloaded file and copy the cos-26Dec2008 \ lib \ cos. jar

5. Copy to lib in the project

6. Restart the web Project

 

Jsp code implementation

FileUpload. jsp

Note that enctype = "multipart/form-data" is used for binary transmission.

<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

HandleFileUpload. jsp

The jsp page called when you click the upload button. The upload function is processed.

<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <% @ page import = "com. oreilly. servlet. multipartRequest "%> <% @ page import =" com. oreilly. servlet. multipart. defaultFileRenamePolicy "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

 

Test the upload function

First visit the upload page http: // localhost: 8081/web01/view/fileUpload. jsp

Click Browse to select a file

Click Upload

File Uploaded

Note: before uploading a file, the directory name should exist in the directory. Otherwise, an error indicating that the directory does not exist will be reported.

 

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.