servlet 获取 post body 体用流读取为空的问题【转】
目前基于rest风格的很多API开始使用通过body data来传输来代替之前的key-value传输方式。在Java servlet或者springmvc中可以通过如下代码来获取并图片通过流方式传输的数据:
InputStream is= null;
String contentStr="";
try {
is = request.getInputStream();
contentStr= IOUtils.toString(is, "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
单纯的如上代码可以获取通过POST或PUT等方法传输的数据,但有时候也有例外,如你现在有如下请求的时候:
POST http://127.0.0.1:8085/test?id=12564564654
POST data: 01001512a101a203a303a
可以看到这里不但会通过post data流的方式发送数据,同时也通过key-value发送了数据,那现在的代码是什么样的呢?如下:
@RequestMapping(value = "posttest", method = RequestMethod.POST)
@ResponseBody
public String test(HttpServletRequest request, @RequestParam(value = "id") String id) {
InputStream is = null;
String contentStr = "";
try {
is = request.getInputStream();
contentStr = IOUtils.toString(is, "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
return contentStr;
}
通过测试你会发现contentStr中并未获取到任何值,同时也不会报任务错误,这是怎么回事呢?会不会是springmvc的问题?答案是否定的。
这个时候获取不到postdata的数据与springmvc没有任何关系,同样的把上面的代码进行修改,如下:
@RequestMapping(value = "posttest", method = RequestMethod.POST)
@ResponseBody
public String test(HttpServletRequest request) {
String id = request.getParameter("id");
InputStream is = null;
String contentStr = "";
try {
is = request.getInputStream();
contentStr = IOUtils.toString(is, "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
return contentStr;
}
经过测试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数据。
最终这个问题的解决方案是把request.getInputStream()放到string id= request.getParameter("id");之前,这样就回避了servlet的规范,代码调整后为:
@RequestMapping(value = "posttest", method = RequestMethod.POST)
@ResponseBody
public String test(HttpServletRequest request) {
InputStream is = null;
String contentStr = "";
try {
is = request.getInputStream();
contentStr = IOUtils.toString(is, "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
String id = request.getParameter("id");
return contentStr;
}
在servlet规范中已经说明是post请求时才会有这个获取post提交数据的顺序问题,而在其它的如put中不会出现这种顺序问题。
servlet 获取 post body 体用流读取为空的问题【转】的更多相关文章
- 转-servlet 获取 post body 体用流读取为空的问题
目前基于rest风格的很多API开始使用通过body data来传输来代替之前的key-value传输方式.在Java servlet或者springmvc中可以通过如下代码来获取并图片通过流方式传输 ...
- java HttpServletRequest 重复流读取
在用reset接口的时候,常常会使用request.getInputStream()方法,但是流只能读取一次,一旦想要加上一个过滤器用来检测用户请求的数据时就会出现异常. 在过滤器中通过流读取出用 ...
- java HttpServletRequest 重复流读取
在用reset接口的时候,常常会使用request.getInputStream()方法,但是流只能读取一次,一旦想要加上一个过滤器用来检测用户请求的数据时就会出现异常. 在过滤器中通过流读取出用 ...
- java IO流读取图片供前台显示
最近项目中需要用到IO流来读取图片以提供前台页面展示,由于以前一直是用url路径的方式进行图片展示,一听说要项目要用IO流读取图片感觉好复杂一样,但任务下达下来了,做为程序员只有选择去执行喽,于是找了 ...
- servlet获取参数时,request.getParameter("id")参数获取失败
servlet获取参数时,request.getParameter("id")参数获取失败,这里的参数是“index”里面href中的参数 要注意,取不到值,是不是要取的参数有没有 ...
- Servlet获取URL地址
这里来说说用Servlet获取URL地址.在HttpServletRequest类里,有以下六个取URL的函数: getContextPath 取得项目名 getServletPath 取得Servl ...
- paip.获取proxool的配置 xml读取通过jdk xml 初始化c3c0在代码中总结
paip.获取proxool的配置 xml读取通过jdk xml 初始化c3c0在代码中 xml读取通过jdk xml 初始化c3c0在代码中.. ... 作者Attilax 艾龙, EMAI ...
- JavaWeb学习记录(八)——servlet获取配置信息
jdbc.properties内容如下: jdbcUrl=jdbc\:mysql\://localhost\:3306/animaluser=rootpass=root servlet获取资源信息代码 ...
- Servlet 获取IllegelStateException
Servlet 获取IllegelStateException: response提交之后,进行requestDispatcher.forwar(),会产生这样的问题: 但是必须是outputStre ...
随机推荐
- 博客用Markdown编辑器插入视频
要展示一些App的效果用或者更方便地展示工具的操作,可以使用视频. 以下有两种方式可以在博客中插入视频 第一种 此方法适用于插入来源优酷的视频或者你自己录制了视频上传到优酷,这种方法的好处是可以插入时 ...
- #Leetcode# 977. Squares of a Sorted Array
https://leetcode.com/problems/squares-of-a-sorted-array/ Given an array of integers A sorted in non- ...
- PAT 1033 旧键盘打字
https://pintia.cn/problem-sets/994805260223102976/problems/994805288530460672 旧键盘上坏了几个键,于是在敲一段文字的时候, ...
- dotnet core 安装
sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc sudo sh -c 'echo -e "[packa ...
- 周刷题第一期总结(two sum and two numbers)
由于深深的知道自己是事件驱动型的人,一直想补强自己的薄弱环节算法,却完全不知道从哪里入手.所以只能采用最笨的办法,刷题.从刷题中遇到问题就解决问题,最后可能多多少少也能提高一下自己的渣算法吧. 暂时的 ...
- python2.7.x的字符串编码到底什么鬼?(中文和英文的处理)
一直以来我其实一直对python的编码弄得非常晕,能正常编码,也能处理一些情况.但是始终不明白有些问题究竟为何出,原因是什么,为什么要这样用. 今天晚上正好好好研究了一番解答了自己心中的困惑. Q:p ...
- SourceTree 如何下载git 管理的代码-如何创建分支,删除分支,提交代码,回退代码
把用户给的链接拿过来,然后输入浏览器,然后在左侧会有Actions 中有个Clone;点击Clone之后,有个 Clone in Source Tree 点击,打开你的本地Source Tree,然后 ...
- python之打印日志logging
import logging # 简单打印日志举例 logging.basicConfig(level=logging.DEBUG) # 设置日志级别,WARN logging.warning('Wa ...
- SQL连接:内连接、外连接、交叉连接。
SQL连接可以分为内连接.外连接.交叉连接. 数据库数据: book表 stu表 1.内连接 ...
- float 、double 加减乘除出现小数位多出的问题
/** * 提供精確的加法運算 * @param args */ public static double add(double v1, double v2) { BigDecimal b1 = ne ...