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。

         当action为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1& amp; amp;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)。

二,使用中需要注意的地方

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

       有两种解法办法:

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

          

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

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值