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.