HTTP uploads a file by post and constructs its request header and message packet application/x-www-form-urlencoded Multipart/form-data

Source: Internet
Author: User
In the syntax of a form element, enctype indicates that the format of the submitted data uses the Enctype property to specify the type of encoding used by the browser when the data is sent back to the server. Below is the description: application/x-www-form-urlencoded: Form data is encoded as a name/value pair. This is the standard encoding format. multipart/form-data: Form data is encoded as a message, and each control on the page corresponds to one part of the message. text/plain: Form data is encoded in plain text without any controls or formatting characters.
Add
The Enctype property of the form is encoded and is commonly used in two ways: application/x-www-form-urlencoded and Multipart/form-data, and the default is application/ X-www-form-urlencoded. When the action is get, the browser converts the form data into a string (Name1=value1&name2=value2 ...) using x-www-form-urlencoded encoding. , and then append the string to the URL and use the? Split to load the new URL. When the action is post, the browser encapsulates the form data into the HTTP body and sends it to the server. If you don't have a type=file control, you can use the default application/x-www-form-urlencoded. But if you have type=file, you will need to use Multipart/form-data. The browser splits the entire form into controls and adds Content-disposition (form-data or file) to each section, Content-type (default is Text/plain), name (control name), and so on. and add a separator (boundary).

Content-type: Encoding Properties

excerpt from Network:1. Overview
In the original HTTP protocol, there was no function to upload files. rfc1867 (http://www.ietf.org/rfc/rfc1867.txt) adds this functionality to the HTTP protocol. Client browsers, such as Microsoft IE, Mozila, Opera, and so on, send user-specified files to the server according to this specification. Server-side Web page programs, such as PHP, ASP, JSP, etc., you can follow this specification, the user sent to resolve the file.
Microsoft IE, Mozila, has supported this agreement by using a special form in the Web page to send files.
Most HTTP servers, including Tomcat, already support this protocol and can accept sent files.
A variety of web programs, such as PHP, ASP, JSP, for uploading files have done a good package. 2, upload the file instance: with Servelet implementation (HTTP server for Tomcat 4.1.24)
1. In an HTML page, write the following form: <form enctype= "Multipart/form-data" action= "Http://192.168.29.65/UploadFile" method= Post> load Multi files:<br> <input name= "userfile1" type= "file" ><br> <input name= "use Rfile2 "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= "submitted" ><input type=reset> </form> 2. The writing of the service-side Servelet
There are now a lot of Third-party http upload file ToolPak. The Jarkata project itself provides FileUpload package http://jakarta.apache.org/commons/fileupload/. File upload, table single processing, efficiency issues are basically taken into account. This package is used in struts, but is encapsulated in a separate way with struts. Here we use the FileUpload package directly. For the use of struts, see the related documentation for struts.
This processing file uploads the Servelet main code as follows:
public void DoPost (HttpServletRequest request, httpservletresponse response) {    diskfileupload DISKFI Leupload = new Diskfileupload ();    //  allows maximum file length     Diskfileupload.setsizemax (100*1024*1024);    //  set Memory buffer size     diskfileupload.setsizethreshold (4096);    //  set up temporary directory     diskfileupload.setrepositorypath ("c:/tmp");     List fileitems = diskfileupload.parserequest (request);     iterator iter = Fileitems.iterator ();     for (; Iter.hasnext ();) {        Fileitem Fileitem = (fileitem) iter.next ();          if (Fileitem.isformfield ()) {            //  is currently a table item             out.println ("form field : "+ fileitem.getfieldname () +","+ fileitem.getstring ());        } else {            //  is currently an uploaded file             String fileName = Fileitem.getname ();             Fileitem.write (New File ("c:/uploads/" + FileName));        }    }} details such as exception handling, file renaming, etc. are not written for brevity.
3, the client sends the content constructs assumes the Web page program which accepts the file is located in Http://192.168.29.65/upload_file/UploadFile.
Let's say we want to send a binary file, a text box form item, and a Password box form item. The file name is e:\s, which reads as follows: (where xxx represents binary data, such as 01 02 03)
A BB XXX CCC client should send the following content to 192.168.29.65: Post/upload_file/uploadfile http/1.1 accept:text/plain, */* accept-language:z H-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 one word, including the last carriage return.
Note: content-length:424 Here 424 is the total length of the red content (including the final carriage return)
Note This line:
Content-type:multipart/form-data; boundary=---------------------------7d33a816d302b6
According to rfc1867, Multipart/form-data is a must.
---------------------------7d33a816d302b6 is a separator, separating multiple files, form items. Where 33a816d302b6 is a number that is generated instantly to ensure that the entire delimiter does not appear in the contents of a file or table item. The front---------------------------7d is a unique logo for IE. Mozila for---------------------------71
Send This example by hand, and test the pass in the servlet above.
(There is a carriage return above) the user can select multiple files, fill out the other items of the form, click the "Submit" button and then start to upload 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 properties are required. Multipart/form-data is a new type of encoding to improve the efficiency of binary file transfer. For specific explanations, see rfc1867

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.