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()在同一个响应中是不能混合使用的,如果混合使用会抛异常的。

     

  • 相关阅读:
    android中图型的阴影效果(shadow-effect-with-custom-shapes)
    git的经常使用命令
    C# vs Java
    Android-68-Tomcat各种启动错误的解决的方法,如:Exception in thread &quot;Thread-6&quot; NoClassDefFoundError,Document base E:
    Java高级程序猿技术积累
    Floodlight下发流表过程分析
    Maximal Rectangle [leetcode] 的三种思路
    C++实现顺序栈的基本功能
    ZOJ 1654 Place the Robots(最大匹配)
    [2-SAT] poj 3207 Ikki&#39;s Story IV
  • 原文地址:https://www.cnblogs.com/liangjq/p/3890784.html
Copyright © 2011-2022 走看看