zoukankan      html  css  js  c++  java
  • HTML中的form表单有一个关键属性 enctype

    HTML中的form表单有一个关键属性 enctype=application/x-www-form-urlencoded 或multipart/form-data。

    1、enctype="application/x-www-form-urlencoded"是默认的编码方式,当以这种方式提交数据时,HTTP报文中的内容是:

    Html代码  收藏代码
    1. <span style="font-size: small;">POST /post_test.php HTTP/1.1   
    2. Accept-Language: zh-CN  
    3. User-Agent: Mozilla/4.0   
    4. Content-Type: application/x-www-form-urlencoded   
    5. Host: 192.168.12.102  
    6. Content-Length: 42  
    7. Connection: Keep-Alive  
    8. Cache-Control: no-cache  
    9.    
    10. title=test&content=%B3%AC%BC%B6%C5%AE%C9%FA&submit=post+article   
    11. </span>  

    Servlet的API提供了对这种编码方式解码的支持,只需要调用ServletRequest 类中的getParameter()方法就可以得到表单中提交的数据。


    2、在传输大数据量的二进制数据时,必须将编码方式设置成enctype="multipart/form-data",当以这种方式提交数据时,HTTP报文中的内容是:

    Html代码  收藏代码
    1. <span style="font-size: small;">POST /post_test.php?t=1 HTTP/1.1  
    2. Accept-Language: zh-CN  
    3. User-Agent: Mozilla/4.0    
    4. Content-Type: multipart/form-data; boundary=---------------------------7dbf514701e8  
    5. Accept-Encoding: gzip, deflate  
    6. Host: 192.168.12.102  
    7. Content-Length: 345  
    8. Connection: Keep-Alive  
    9. Cache-Control: no-cache  
    10.    
    11. -----------------------------7dbf514701e8  
    12. Content-Disposition: form-data; name="title"  
    13. test  
    14. -----------------------------7dbf514701e8  
    15. Content-Disposition: form-data; name="content"  
    16. ....  
    17. -----------------------------7dbf514701e8  
    18. Content-Disposition: form-data; name="submit"  
    19. post article  
    20. -----------------------------7dbf514701e8--</span>  

    如果以这种方式提交数据就要用request.getInputStream()或request.getReader()来获取提交的数据 用 request.getParameter()是获取不到提交的数据的。

     

     

    最后注意request.getParameter()、request.getInputStream()、request.getReader()这三种方法是有冲突的,因为流只能被读一次。
    比如:
    当 form表单内容采用enctype=application/x-www-form-urlencoded编码时,先通过调用 request.getParameter()方法获取数据后,再调用request.getInputStream()或 request.getReader()已经获取不到流中的内容了,因为在调用 request.getParameter()时系统可能对表单中提交的数据以流的形式读了一次,反之亦然。

     

    当 form表单内容采用enctype=multipart/form-data编码时,调用request.getParameter()获取不到数据, 即使已经调用了request.getParameter()方法也可以再通过调用request.getInputStream()或 request.getReader()获取表单中的数据,但request.getInputStream()和 request.getReader()在同一个响应中是不能混合使用的,如果混合使用会抛异常的。

     

  • 相关阅读:
    ios9没事,ios7tableviewcell报约束冲突问题
    在导航控制器的rootviewcontroller中push一个控制器 出现view重叠现象
    用mansard对cell的子控件设置约束,并且自动计算cell高度的问题,ios7警告
    textview第一次出现不可滚动文本,但是点击出现键盘,键盘落下,就可以滚动问题
    animateWithDuration 这样的方法,在第二次应用到某个view上动画时间无效问题
    UItoolBar 设置背景颜色
    implicitly declaring library function 'objc_msgSend'with type '(id,SEL,...)' 警告
    侧滑手势移除控制器,pop
    textfield光标消失和故意隐藏需求
    【Debug】IAR右键无法跳转到定义的解决方法
  • 原文地址:https://www.cnblogs.com/liangjq/p/3890784.html
Copyright © 2011-2022 走看看