Form表单的enctype

基础form表单

<form action="/submit" enctype="text/plain" method="POST">
    <p>关键字: <input type="text" name="keyword" /></p>
    <input type="submit" value="Submit" />
</form>

enctype值和意义

  • application/x-www-form-urlencoded:在发送前编码所有字符(默认)
  • multipart/form-data:不对字符编码。在使用包含文件上传控件的表单时,必须使用该值。
  • text/plain:空格转换为 "+" 加号,但不对特殊字符编码。

说明

HTTP请求中,如果是get请求,那么表单参数以key1=value1&key2=value2的形式附到url的后面,如果是post请求,那么表单参数是在请求体中,也是以key1=value1&key2=value2的形式在请求体中。

post请求的Content-Type为application/x-www-form-urlencoded,参数是在请求体中,即上面请求中的Form Data。后端servlet可以通过request.getParameter("keyword")获取数据

Content-Type为text/plain;charset=UTF-8,则请求表单参数在RequestPayload中,后端servlet可以通过org.apache.commons.io.IOUtils.toString(request.getReader())获取数据。

参考

原文地址:https://www.cnblogs.com/powercto/p/6953176.html