[Reprint]http upload a file by post, construct its request header and message packet

Source: Internet
Author: User
Tags exception handling rfc
Suppose the Web page program that accepts the file is located in Http://192.168.29.65/upload_file/UploadFile. Suppose we want to send a picture file named "Kn.jpg",

After the client links are 192.168.24.56, you should send the following HTTP request:

Post/logsys/home/uploadispeedlog!dodefault.html http/1.1

Accept:text/plain, */*
Accept-language:zh-cn
host:192.168.24.56
content-type:multipart/form-data;boundary=-----------------------------7db372eb000e2
User-agent:winhttpclient
content-length:3693
Connection:keep-alive

-------------------------------7db372eb000e2

Content-disposition:form-data; Name= "File"; Filename= "Kn.jpg"

Content-type:image/jpeg

(Omit JPEG file binary data here ... )

-------------------------------7db372eb000e2--

This content must be one word, including the last carriage return, and the red font part is the head of the protocol. Upload data to the server, not the protocol header each field must be explained, where content-type is necessary, it includes a similar flag of the nature of the name boundary, it can be arbitrary input string. It is also necessary to follow the specific content. It is used to distinguish the beginning of a piece of content. content-length:3693, here's 3693 is to upload the total length of the file. The green font part is the data that needs to be uploaded, can be the text, can also be the picture and so on. The data content needs to be preceded by Content-disposition, Content-type, and content-transfer-encoding, and other description fields. The final purple part is the end of the agreement.

Note This line:

Content-type:multipart/form-data; boundary=---------------------------7db372eb000e2

According to rfc1867, Multipart/form-data is a must.
---------------------------7db372eb000e2 is a separator, separating multiple files, form items. Where B372eb000e2 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. Each part of the form is separated by a delimiter, which must precede the delimiter with "--" two characters (that is,--{boundary}) to be the delimiter that the HTTP protocol considers to be a form, meaning that the end phrase is added "--" after the correct delimiter to indicate the end.

The front---------------------------7d is an IE-specific logo, Mozila for---------------------------71.

  

Each delimited data can be content-type to represent the type of the following data, refer to rfc1341 (http://www.ietf.org/rfc/rfc1341.txt)

For example: Contect-type:image/jpeg indicates that the following data is JPEG file data




============================================================================


[go] upload files via HTTP protocol

2008-04-30 23:55

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

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.