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

     

  • 相关阅读:
    【Python】在控制台输出不同颜色的文字
    【python】如何去掉使用BeautifulSoup读取html出现的警告UserWarning: You provided Unicode markup but also provided a value for from_encoding
    【nodejs】修改了下对股票表进行crud操作的代码
    【nodejs】用express又做了份crud
    day20_day23课堂笔记
    Myeclipse中java web.xml报错cvc-complex-type.2.3: Element 'web-app' cannot have character [children], because the type's content type is element- only.
    html中让input标签只读不可编辑的方法
    Win10锁屏壁纸位置在哪? 默认锁屏壁纸怎么提取?
    Potplayer怎么实现视频的镜面翻转?
    How to export Excel files in a Python/Django application
  • 原文地址:https://www.cnblogs.com/liangjq/p/3890784.html
Copyright © 2011-2022 走看看