Upload files over HTTP (rfc1867 protocol overview, JSP application example, client-sent content Construction)

Source: Internet
Author: User

1. Overview
In the original HTTP protocol, there was no file upload function. Rfc1867 (http://www.ietf.org/rfc/rfc1867.txt) added this feature for the HTTP protocol. Client browsers, such as Microsoft IE, ILA Ila, and opera, send user-specified files to the server according to this specification. Server webpageProgram, Such as PHP, ASP, JSP, etc. files sent by users can be parsed according to this specification.
Microsoft IE, ILA Ila, and opera already support this protocol. You can use a special form on the webpage to send files.
Most HTTP servers, including tomcat, support this protocol and can send files.
Various web programs, such as PHP, ASP, and JSP, have encapsulated the uploaded files.

2. File Upload instance: Implemented Using servelet (HTTP server is Tomcat 4.1.24)
1. Write the following form in an HTML webpage:

<FormEnctype = "multipart/form-Data"Action = "http: // 192.168.29.65/uploadfile"Method = post>

Load multi files: <br>

<Input name = "userfile1"Type = "file"> <Br>

<Input name = "userfile2" type = "file"> <br>

<Input name = "userfile3" type = "file"> <br>

<Input name = "userfile4" type = "file"> <br>

Text Field: <input type = "text" name = "text" value = "text"> <br>

<Input type = "Submit" value ="Submit"> <Input type = reset>

</Form>

 

2. Write server servelet
Currently, there are many third-party HTTP Upload File tool libraries. The jarkata project itself provides fileupload package http://jakarta.apache.org/commons/fileupload. File Upload, form Item Processing, and efficiency are all taken into account. This package is used in struts, but it is encapsulated separately in struts mode. Here we use the fileupload package directly. For the usage of struts, see the struts documentation.
This mainly processes the servelet of file uploads.CodeAs follows:

Public void dopost (httpservletrequest request, httpservletresponse response ){

Diskfileupload = new diskfileupload ();

//Maximum file length allowed

Diskfileupload. setsizemax (100*1024*1024 );

//Set memory buffer size

Diskfileupload. setsizethreshold (4096 );

//Set temporary directory

Diskfileupload. setrepositorypath ("C:/tmp ");

 

List fileitems = diskfileupload. parserequest (request );

Iterator iter = fileitems. iterator ();

For (; ITER. hasnext ();){

Fileitem = (fileitem) ITER. Next ();

If (fileitem. isformfield ()){

//It is currently a form item

Out. println ("form field:" + fileitem. getfieldname () + "," + fileitem. getstring ());

} Else {

//This is an uploaded file.

String filename = fileitem. getname ();

Fileitem. Write (new file ("C:/uploads/" + filename ));

}

}

}

 

 

For the sake of simplicity, details such as exception handling and file rename are not provided.

3. Construct the content sent by the client

Assume that the webpage program that receives the file is located at http: // 192.168.29.65/upload_file/uploadfile.
Suppose we want to send a binary file, a text box form item, and a password box form item. The file name is E: \ s, and the content is as follows: (xxx indicates binary data, for example, 01 02 03)

A

Bb

Xxx

CCC

 

 

The client should192.168.29.65Send the following content:

 

Post/upload_file/uploadfile HTTP/1.1

Accept: text/plain ,*/*

Accept-language: ZH-CN

HOST: 192.168.29.65: 80

Content-Type: multipart/form-data; boundary = --------------------------- 7d33a816d302b6

User-Agent: Mozilla/4.0 (compatible; OpenOffice.org)

Content-Length: 424

Connection: keep-alive

 

----------------------------- 7d33a816d302b6

Content-Disposition: Form-data; name = "userfile1 ";Filename = "E: \ s"

Content-Type: Application/octet-stream

 

A

Bb

Xxx

CCC

----------------------------- 7d33a816d302b6

Content-Disposition: Form-data; name = "text1"

 

Foo

----------------------------- 7d33a816d302b6

Content-Disposition: Form-data; name = "password1"

 

Bar

----------------------------- 7d33a816d302b6 --

 

 

This content must be a single character, including the last carriage return.
Note: Content-Length: 424 here 424 is the total length of the Red content (including the last carriage return)
Note This line:
Content-Type: multipart/form-data; boundary = --------------------------- 7d33a816d302b6
According to rfc1867, multipart/form-data is required.
--------------------------- 7d33a816d302b6 is a delimiter that separates multiple files and form items. 33a816d302b6 is a number generated in real time to ensure that the entire separator does not appear in the content of the file or form item. The previous --------------------------- 7D is a unique identifier of IE. Mozila is --------------------------- 71
This example is manually sent and passed the test in the servlet above.

(There is a carriage return above)

 

 

You can select multiple files and fill in other items in the form. Click "Submit" to upload the files to http: // 192.168.29.65/upload_file/uploadfile. This is a servelet program.
Note enctype = "multipart/form-Data", method = post, type = "File ". According to rfc1867, these three attributes are required. Multipart/form-data is a new encoding type to improve the transmission efficiency of binary files. For more information, see rfc1867.

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.