request.getParameter()获取不到数据的问题
2017-02-08 09:24
381 查看
最近做项目时,发现手机客户端通过http协议post方式上传数据到服务端,在服务器端通过request.getInputStream()能获取到相应的数据,但用request.getParameter()却获取不到数据。这是怎么回事呢,后来发现这种情况跟form表单的属性 enctype有关系。
HTML中的form表单有一个关键属性 enctype=application/x-www-form-urlencoded 或multipart/form-data。
1、enctype="application/x-www-form-urlencoded"是默认的编码方式,当以这种方式提交数据时,HTTP报文中的内容是:
Html代码
<span style="font-size: small;">POST /post_test.php HTTP/1.1
Accept-Language: zh-CN
User-Agent: Mozilla/4.0
Content-Type: application/x-www-form-urlencoded
Host: 192.168.12.102
Content-Length: 42
Connection: Keep-Alive
Cache-Control: no-cache
title=test&content=%B3%AC%BC%B6%C5%AE%C9%FA&submit=post+article
</span>
Servlet的API提供了对这种编码方式解码的支持,只需要调用ServletRequest 类中的getParameter()方法就可以得到表单中提交的数据。
2、在传输大数据量的二进制数据时,必须将编码方式设置成enctype="multipart/form-data",当以这种方式提交数据时,HTTP报文中的内容是:
Html代码
<span style="font-size: small;">POST /post_test.php?t=1 HTTP/1.1
Accept-Language: zh-CN
User-Agent: Mozilla/4.0
Content-Type: multipart/form-data; boundary=---------------------------7dbf514701e8
Accept-Encoding: gzip, deflate
Host: 192.168.12.102
Content-Length: 345
Connection: Keep-Alive
Cache-Control: no-cache
-----------------------------7dbf514701e8
Content-Disposition: form-data; name="title"
test
-----------------------------7dbf514701e8
Content-Disposition: form-data; name="content"
....
-----------------------------7dbf514701e8
Content-Disposition: form-data; name="submit"
post article
-----------------------------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()在同一个响应中是不能混合使用的,如果混合使用会抛异常的。
HTML中的form表单有一个关键属性 enctype=application/x-www-form-urlencoded 或multipart/form-data。
1、enctype="application/x-www-form-urlencoded"是默认的编码方式,当以这种方式提交数据时,HTTP报文中的内容是:
Html代码
<span style="font-size: small;">POST /post_test.php HTTP/1.1
Accept-Language: zh-CN
User-Agent: Mozilla/4.0
Content-Type: application/x-www-form-urlencoded
Host: 192.168.12.102
Content-Length: 42
Connection: Keep-Alive
Cache-Control: no-cache
title=test&content=%B3%AC%BC%B6%C5%AE%C9%FA&submit=post+article
</span>
Servlet的API提供了对这种编码方式解码的支持,只需要调用ServletRequest 类中的getParameter()方法就可以得到表单中提交的数据。
2、在传输大数据量的二进制数据时,必须将编码方式设置成enctype="multipart/form-data",当以这种方式提交数据时,HTTP报文中的内容是:
Html代码
<span style="font-size: small;">POST /post_test.php?t=1 HTTP/1.1
Accept-Language: zh-CN
User-Agent: Mozilla/4.0
Content-Type: multipart/form-data; boundary=---------------------------7dbf514701e8
Accept-Encoding: gzip, deflate
Host: 192.168.12.102
Content-Length: 345
Connection: Keep-Alive
Cache-Control: no-cache
-----------------------------7dbf514701e8
Content-Disposition: form-data; name="title"
test
-----------------------------7dbf514701e8
Content-Disposition: form-data; name="content"
....
-----------------------------7dbf514701e8
Content-Disposition: form-data; name="submit"
post article
-----------------------------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()在同一个响应中是不能混合使用的,如果混合使用会抛异常的。
相关文章推荐
- request.getParameter()获取不到数据的问题
- request.getParameter()获取不到数据的问题
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- request.getParameter 获取不到页面的参数的问题
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- request.getParameter()获取不到数据的问题
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- kindeditor上传文件在SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题 .
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- SpringMVC中servletFileUpload.parseRequest(request)解析为空获取不到数据问题
- MVC——应用Ajax获取不到数据问题解答
- 使用postman请求springmvc接口时,request获取不到数据
- ASIHTTPRequest 在release(打包)模式下数据获取或post失败问题
- 解决apache/nginx做反向代理导致request.getServerName()外网环境获取不到代理地址(或域名)的问题
- 解决apache/nginx做反向代理导致request.getServerName()外网环境获取不到代理地址(或域名)的问题