File upload and download

Source: Internet
Author: User
 

To upload files in Wen development, you need to complete the following two steps:

1. Add an upload entry (JSP page or HTML page) to the web page)

2. Read the uploaded data in the servlet and save it to the local hard disk.

Add an upload entry to the web page:

<Input type = "file"> the tag is used to add an input entry for file upload on the web page;

Note:

1) You must set the name attribute of the input item. Otherwise, the browser will not send the data of the uploaded file.

2) The enctype attribute value of form must be set to multipart/form-data. after this value is set, the browser attaches the file data to the HTTP request message and uses the mime protocol to describe the uploaded file, to facilitate the recipient's data parsing and processing.

Example:

<Form action = "$ {pagecontext. Request. contextpath}/servlet/uploadservlet" method = "post" enctype = "multipart/form-data">

</Form>

Read the file and upload data in the servlet and save it to the local hard disk:

The request object provides a getinputstream method to read the data submitted by the client. However, since users may upload multiple files at the same time, it is very troublesome to program on the servlet side to directly read and upload data, and parse the corresponding file data separately.

To facilitate user processing of File Upload data, the appache open-source organization provides an open-source organization (Commons-fileupload) that processes form file uploads. This organization has excellent performance and is easy to use using APIs, developers can easily implement the Web file upload function. Therefore, the file upload function is usually implemented using the commons-fileupload component in Web development.

Core API-disfileitemfactory:

Diskfileitemfactory is the factory for creating fileitem objects.

Common methods include:

Public void setsizethreshold (INT sizethreshold): Set the memory buffer size. The default value is 10 KB. When the size of the uploaded file is greater than the buffer size,
The fileupload component uses the Temporary File Cache to upload files.

Public void setrepository (Java. Io. File Repository): Specifies the temporary file directory. The default value is system. getproperty ("Java. Io. tmpdir ").

Public diskfileitemfactory (INT sizethreshold, java. Io. File Repository): Constructor

Core API-servletfileupload:

Servletfileupload processes uploaded file data and encapsulates each input item in the form into a fileitem object. Common methods include:

Boolean ismultipartcontent (httpservletrequest request): determines whether the upload form is of the multipart/form-data type.

List parserequest (httpservletrequest request): parses the request object, packs each input item in the form into a fileitem object, and returns a list set that stores all fileitems.

Setfilesizemax (long filesizemax): sets the maximum value of the uploaded file.

Setsizemax (long sizemax): sets the maximum number of uploaded files.

setheaderencoding (Java. Lang. String encoding): sets the encoding format.

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.