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

the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header浏览器

 

一,HTTP上传的基本知识      服务器

         在Form元素的语法中,EncType代表提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型。下边是说明: application/x-www-form-urlencoded: 窗体数据被编码为名称/值对。这是标准的编码格式。 multipart/form-data: 窗体数据被编码为一条消息,页上的每一个控件对应消息中的一个部分。 text/plain:窗体数据以纯文本形式进行编码,其中不含任何控件或格式字符。
补充
form的enctype属性为编码方式,经常使用有两种:application/x-www-form-urlencoded和multipart/form-data,默认为application /x-www-form-urlencoded。app

         当action为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1& amp; amp;name2=value2...),而后把这个字串append到url后面,用?分割,加载这个新的url。post

         当action为post时候,浏览器把form数据封装到http body中,而后发送到server。编码

        若是没有type=file的控件,用默认的application/x-www-form-urlencoded就能够了。可是若是有 type=file的话,就要用到multipart/form-data了。浏览器会把整个表单以控件为单位分割,并为每一个部分加上 Content-Disposition(form-data或者file),Content-Type(默认为text/plain),name(控件 name)等信息,并加上分割符(boundary)。url

二,使用中须要注意的地方spa

       在AJAX往服务器上传数据是,设置了content-type为application/x-www-form-urlencoded,此时是对整个发 送内容做了编码,并非对名字对应的值作了编码。所以,在服务器端,经过request.getParameter("name")的方式取值,是有问题 的。code

       有两种解法办法:orm

       1)改服务器端: 采用流的方式硬编码server

          

        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)改客户端:更改数据发送结构

             在往服务器上发数据的时候,使用name=escape(value)的方式组对

              此时在服务器代码中,经过request.getParameter("name")得到的数值,就不用编码了

 

当action为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1&name2=value2...),而后把这个字串append到url后面,用?分割,加载这个新的url。 
当action为post时候,浏览器把form数据封装到http body中,而后发送到server。 

若是没有type=file的控件,用默认的application/x-www-form-urlencoded就能够了。 
可是若是有type=file的话,就要用到multipart/form-data了。浏览器会把整个表单以控件为单位分割,并为每一个部分加上Content-Disposition(form-data或者file),Content-Type(默认为text/plain),name(控件name)等信息,并加上分割符(boundary)。 
若是有如下form,并选择了file1.txt上传 

<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> 

 

则有以下body: 

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--
相关文章
相关标签/搜索