servlet 获取 post body 体 (用流读取为空的问题)

来源:互联网 发布:小学生绘画软件下载 编辑:程序博客网 时间:2024/05/01 03:24


  目前基于rest风格的很多API开始使用通过body data来传输来代替之前的key-value传输方式。在java servlet或者springmvc中可以通过如下代码来获取并图片通过流方式传输的数据:

 

1InputStream is= null; String contentStr=""
2try {
3     is = request.getInputStream();
4          contentStr= IOUtils.toString(is, "utf-8");
5   catch (IOException e) {
6      e.printStackTrace(); 
7  }


 

 

单纯的如上代码可以获取通过POST或PUT等方法传输的数据,但有时候也有例,如果你现在有如下请求的时候:

 

POST http://127.0.0.1:8085/test?id=12564564654

POST data:

01001512a101a203a303a

 

可以看到这里不但会通过post data流的方式发送数据,同时也通过key-value发送了数据,那现在的代码是什么样的呢?如下:

01@RequestMapping(value = "posttest", method = RequestMethod.POST) 
02@ResponseBody 
03public String test(HttpServletRequest request,@RequestParam(value = "id") String id ) {
04     InputStream is= null;     
05     String contentStr="";     
06     try {         
07         is = request.getInputStream();         
08         contentStr= IOUtils.toString(is, "utf-8");     
09         catch (IOException e) { 
10                 e.printStackTrace();     
11         }     
12         return contentStr; 
13}


 

通过测试你会发现contentStr中并未获取到任何值,同时也不会报任务错误,这是怎么回事呢?会不会是springmvc的问题?答案是否定的。这个时候获取不到postdata的数据与springmvc没有任务关系,同样的把上面的代码进行修改,如下:


01@RequestMapping(value = "posttest", method = RequestMethod.POST) 
02@ResponseBody
03public String test(HttpServletRequest request) {
04     String id=request.getParameter("id");     
05     InputStream is= null;     
06     String contentStr="";     
07     try 
08             is = request.getInputStream();         
09             contentStr= IOUtils.toString(is, "utf-8");     
10         catch (IOException e) {
11                  e.printStackTrace();     
12         }     
13         return contentStr; 
14}


 

经过测试getInputStream仍然没有数据。

 

那问题出在哪呢?经过查找终于找到问了问答的原因,答案在servlet规范中:

3.1.1 When Parameters Are Available
The following are the conditions that mustbe met before post form data will be 
populated to the parameter set:
1. The request is an HTTP or HTTPS request.
2.  The HTTP method is POST.
3.  The content type is application/x-www-form-urlencoded.
4.  The servlet has made an initial call of any of the getParameterfamily of methods 
on the request object.
If the conditions are not met and the post form data is not included in the parameter 
set, the post data must still be available to the servlet via the request object’s input 
stream. If the conditions are met, post form data will no longer be available for 
reading directly from the request object’s input stream.

 

经过翻译servlet上面一段规范如下:

根据Servlet规范,如果同时满足下列条件,则请求体(Entity)中的表单数据,将被填充到request的parameter集合中(request.getParameter系列方法可以读取相关数据):
1 这是一个HTTP/HTTPS请求
2 请求方法是POST(querystring无论是否POST都将被设置到parameter中)
3 请求的类型(Content-Type头)是application/x-www-form-urlencoded
4 Servlet调用了getParameter系列方法


如果上述条件没有同时满足,则相关的表单数据不会被设置进request的parameter集合中,相关的数据可以通过request.getInputStream()来访问。反之,如果上述条件均满足,相关的表单数据将不能再通过request.getInputStream()来读取。

 

根据这个规范的说明,当我们在调用request.getParameter(“id”)的方法时,通过post data交的数据(请求体)被填充到了parameter集合中了,所以后面的通过request.getInputStream获取数据流时就为空。通过测试打印断点我们可以看到如下图:

原本我们只提交了一个参数id,但是在执行过request.getParameter(“id”)的方法后,我们的parameterNames里就有了两个参数,其中第二个参数的key就是我们通过body请求体传输的数据,值也是body请求体的post data数据。

 

最终这个问题的解决方案是把string id= request.getParameter(“id”);放到request.getInputStream之后,这样就回避了servlet的规范,代码调整后为:


01@RequestMapping(value = "posttest", method = RequestMethod.POST) 
02@ResponseBody
03public String test(HttpServletRequest request) {
04     InputStream is= null;     
05     String contentStr="";     
06     try {         
07         is = request.getInputStream();         
08         contentStr= IOUtils.toString(is, "utf-8");     
09         catch (IOException e) {
10                  e.printStackTrace();     
11             }     
12       String id=request.getParameter("id");     
13  return contentStr; 
14}


 

在servlet规范中已经说明是post请求时才会有这个获取post提交数据的顺序问题,而在其它的如put中不会出现这种顺序问题。

0 0
原创粉丝点击