The request doesn't contain a multipart/form-data or multipart/form-data stream, content type header

Source: Internet
Author: User

The request doesn't contain a multipart/form-data or multipart/form-data stream, content type header

 

I. Basic knowledge about HTTP upload

In the form Element syntax, enctype indicates the encoding type used by the browser when the data is sent back to the server using the enctype attribute. Application/X-WWW-form-urlencoded: the form data is encoded as a name/value pair. This is the standard encoding format. Multipart/form-data: the form data is encoded as a message. Each control on the page corresponds to a part of the message. Text/plain: the form data is encoded in plain text format, which does not contain any controls or format characters.
Supplement
The enctype attribute of form is encoded in two ways: Application/X-WWW-form-urlencoded and multipart/form-data, the default value is application/X-WWW-form-urlencoded.

When the action is get, the browser uses the X-WWW-form-urlencoded encoding method to convert form data into a string (name1 = value1 & amp; name2 = value2 ...), then append the string to the end of the URL, using? Load the new URL.

When the action is post, the browser encapsulates form data into the HTTP body and sends it to the server.

If there is no type = file control, use the default application/X-WWW-form-urlencoded. However, if type = file exists, you need to use multipart/form-data. The browser splits the entire form into controls and adds content-disposition (Form-data or file) and Content-Type (text/plain by default) to each part ), name (Control name) and other information, plus the delimiter (Boundary ).

2. Notes for use

When Ajax uploads data to the server, the Content-Type is set to application/X-WWW-form-urlencoded, and the entire sent content is encoded, it does not encode the value corresponding to the name. Therefore, on the server side, the request. getparameter ("name") method is incorrect.

There are two solutions:

1) Change the server side: Use stream-based hard Encoding

        InputStream stream = request.getInputStream();        InputStreamReader isr = new InputStreamReader(stream);        BufferedReader br = new BufferedReader(isr);        String str = br.readLine();        System.out.println(str);        str = URLDecoder.decode(str, "gb2312");        System.out.println(str);        br.close();

 

2) Change the client: change the data sending Structure

When sending data to the server, use name = escape (value) to group

In the server code, the value obtained through request. getparameter ("name") does not need to be encoded.

 

When the action is get, the browser uses the X-WWW-form-urlencoded encoding method to convert form data into a string (name1 = value1 & name2 = value2 ...), then append the string to the end of the URL, using? Load the new URL.
When the action is post, the browser encapsulates form data into the HTTP body and sends it to the server.

If there is no type = file control, use the default application/X-WWW-form-urlencoded.
However, if type = file exists, you need to use multipart/form-data. The browser splits the entire form into controls and adds content-disposition (Form-data or file) and Content-Type (text/plain by default) to each part ), name (Control name) and other information, plus the delimiter (Boundary ).
If you select file1.txt for upload

<form action="http://server.com/cgi/handle"        enctype="multipart/form-data"        method="post">    <input type="text" name="submit-name" value="chmod777"><br />    What files are you sending? <input type="file" name="files"><br /> </form> 

 

The following body is available:

Content-Type: multipart/form-data; boundary=AaB03x    --AaB03x    Content-Disposition: form-data; name="submit-name"    chmod777    --AaB03x    Content-Disposition: form-data; name="files"; filename="file1.txt"    Content-Type: text/plain    ... contents of file1.txt ...    --AaB03x--

 

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.