Upload file multipart form-data boundary instructions for uploading files multipart form-data boundary description

Meaning ENCTYPE = "multipart / form-data" Description:

Overview http protocol to upload files rfc1867 agreement, the client sends the content structure.

Outline                                                                                           

In the first http protocol, no functional aspects upload files. rfc1867 add this functionality to the http protocol. The client's browser, such as Microsoft IE, Mozila, Opera, etc., according to this specification will be sent to the user specified file server. Web server program, such as php, asp, jsp, etc., in accordance with this specification, the user parsed files transmitted. Microsoft IE, Mozila, Opera already support this protocol, you can send a file using a special form in the web page. Most of http server, including tomcat, already support this agreement, acceptable to send a file. A variety of Web applications, such as php, asp, jsp, for uploading files has done a very good package.

Examples                                                                                           

<form action="upFile.php" enctype="multipart/form-data" method="post">
<input name="myflie" type="file" />
<input type="submit" />

Note enctype = "multipart / form-data", method = post, type = "file". According to rfc1867, these three attributes is necessary. multipart / form-data encoding type is new, to improve the transmission efficiency of the binary file.

data transmission                                                                                      

Format must be verbatim, including the last carriage.

Note: Content-Length: 226 226 here is the total length of the red content (including the last carriage return)

Note this line:

Content-Type: multipart/form-data; boundary=-----------------------------264141203718551

The rfc1867, multipart / form-data is required .--------------------------- 7d33a816d302b6 is a separator, for separating multiple, tables individual.

Wherein 7d33a816d302b6 instant string generated to ensure that the entire separator does not appear in the contents of the individual file or table.

Send data using POST                                                                      

POST method to send data mainly to transmit a larger amount of data the client to the server, it is not the URL length limit. POST request to URL-encoded data in the form of the HTTP body, the field in the form fieldname = value, each field separated by &. Note that all the fields are treated as strings. In fact, we need to do is to simulate a form POST browser. The following is a login form IE transmitted POST request:

Copy the code
POST http://127.0.0.1/login.do HTTP/1.0
Accept: image/gif, image/jpeg, image/pjpeg, */*
Accept-Language: en-us,zh-cn;q=0.5
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
Content-Length: 28
 
username=admin&password=1234
Copy the code

To simulate the browser sends the POST request MIDP application, first set HttpConnection request mode is POST (PHP to curl):

// use the POST method 
              httpURLConnection.setRequestMethod ( "POST");

Then construct the HTTP body:

byte[] data = "username=admin&password=1234".getBytes();

And calculating the length of the body, filled Content-Type and Content-Length:

httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length));

Then open OutputStream to write the text:

OutputStream output = hc.openOutputStream();
output.write(data);

It should be noted that the data still needs to be URL encoding format, because the MIDP library is not in the corresponding J2SE class URLEncoder, therefore, need to prepare yourself the encode () method, you can refer to the java.net.URLEncoder.java Source. The rest is reading server response codes coincide with GET, not described in detail here.

Using the multipart / form-data transmission file                                             

If you want to upload files to the server, we have to simulate a POST multipart / form-data type of request MIDP client, Content-Type must be multipart / form-data.

In multipart / form-data format encoded POST request application / x-www-form-urlencoded completely different, multipart / form-data need to be provided in the HTTP request header a separator, e.g. ABCD:

httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=ABCD");

Partition - "separator", then the last one, with each field "- Separator -" indicates the end. For example, to upload a title field "Today" and a file C: \ 1.txt, HTTP text as follows:

Copy the code
--ABCD
Content-Disposition: form-data; name="title"
\r\n
Today
--ABCD
Content-Disposition: form-data; name="1.txt"; filename="C:\1.txt"
Content-Type: text/plain
\r\n
<这里是1.txt文件的内容>
--ABCD--
\r\n
Copy the code

Note that each row must be \ r \ n the end, including the last line. If the program detects IE Sniffer POST request transmitted discovery IE can be similar to separator --------------------------- 7d4a6d158c9, which is IE a random number generated in order to prevent occurrence upload file delimiter identification file server fails to correct starting position. We can write a fixed delimiter, just enough complexity to.

POST codes transmitted file is as follows:

Copy the code
String [] props = ... // field name 
String [] values = ... // field value 
byte [] file = ... // contents of the file 
String BOUNDARY = "---------- ----------------- 7d4a6d158c9 "; // separator 
the StringBuffer the StringBuffer new new SB = (); 
// send each field: 
for (int I = 0; I 
SB = SB .append ( "-"); 
SB = sb.append (the BOUNDARY); 
SB = sb.append ( "\ R & lt \ n-"); 
SB = sb.append ( "the Content-Disposition: Data-form; name = \ "" The props + [I] + "\" \ R & lt \ n-\ R & lt \ n-"); 
SB = sb.append (the URLEncoder.encode (values [I])); 
SB = sb.append (" \ R & lt \ n- "); 
} 
// send file: 
SB = sb.append (" - "); 
SB = sb.append (the BOUNDARY); 
SB = sb.append (" \ R & lt \ n-"); 
SB = sb.append ( "Content-Disposition: form-data; name=\"1\"; filename=\"1.txt\"\r\n"); 
SB = sb.append ( "Content-Type: application/octet-stream\r\n\r\n");
byte[] data = sb.toString().getBytes();
byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
// 设置HTTP头:
hc.setRequestProperty("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY);
hc.setRequestProperty("Content-Length", String.valueOf(data.length + file.length + end_data.length));
// 输出:
output = hc.openOutputStream();
output.write(data);
output.write(file);
output.write(end_data);
// 读取服务器响应:
// TODO...
Copy the code

 

 

Reference: upload files multipart form-data boundary description

Guess you like

Origin www.cnblogs.com/aspirant/p/11240502.html