Request.getInputStream()读取数据遇到的问题
来源:互联网 发布:linux ftp server 编辑:程序博客网 时间:2024/06/06 03:33
- Servlet的Request.getInputStream()只能读取一次问题
查看接口ServletRequest的源码
publicServletInputStreamgetInputStream()throwsIOException;
request.getInputStream()返回的其实是ServletInputStream。ServletInputStream继承了InputStream
但是没有重写reset()方法。
InputStream的reset()方法源码是这样的:
publicsynchronizedvoidreset()throwsIOException {
thrownewIOException("mark/reset not supported");
}
调用reset方法直接抛出异常,所以ServletInputStream是不能调用reset方法,这就导致了只能调用一次getInputStream(),第二次调用的时候没有办法获取到InputStream流中的原因
参考: http://zhangbo-peipei-163-com.iteye.com/blog/2022460
2 request.getInputStream()获取数据失败
根据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()来读取。
参考:http://blog.csdn.net/software_kid/article/details/50435422
参考文档: Servlet Specifiaction 3.0:
3.ServletRequest的getReader()和getInputStream()两个方法只能被调用一次,而且不能两个都调用。
- /**
- * Retrieves the body of the request as binary data using
- * a {@link ServletInputStream}. Either this method or
- * {@link #getReader} may be called to read the body, not both.
- *
- * @return a {@link ServletInputStream} object containing
- * the body of the request
- *
- * @exception IllegalStateException if the {@link #getReader} method
- * has already been called for this request
- *
- * @exception IOException if an input or output exception occurred
- *
- */
- public ServletInputStream getInputStream() throws IOException;
- /**
- * Retrieves the body of the request as character data using
- * a <code>BufferedReader</code>. The reader translates the character
- * data according to the character encoding used on the body.
- * Either this method or {@link #getInputStream} may be called to read the
- * body, not both.
- *
- *
- * @return a <code>BufferedReader</code>
- * containing the body of the request
- *
- * @exception UnsupportedEncodingException if the character set encoding
- * used is not supported and the
- * text cannot be decoded
- *
- * @exception IllegalStateException if {@link #getInputStream} method
- * has been called on this request
- *
- * @exception IOException if an input or output exception occurred
- *
- * @see #getInputStream
- *
- */
- public BufferedReader getReader() throws IOException;
两个方法都注明方法只能被调用一次,由于RequestBody是流的形式读取,那么流读了一次就没有了,所以只能被调用一次。既然是因为流只能读一次的原因,那么只要将流的内容保存下来, 就可以实现反复读取了。
4.解决 getInputStream读取多次的办法
参考 http://liwx2000.iteye.com/blog/1542431
阅读全文
0 0
- Request.getInputStream()读取数据遇到的问题
- request.getInputStream()读取post传递的xml文件问题
- Servlet的Request.getInputStream()只能读取一次问题
- Servlet的Request.getInputStream()只能读取一次问题
- 关于request.getinputStream读取一次的问题研究
- 解决request.getInputStream()只能读取一次的问题
- Servlet的Request.getInputStream()只能读取一次
- 关于request.getInputStream和request.getReader 方法的一些区别以及遇到的一些问题
- request.getInputStream()只能获取一次的问题
- request.getInputStream()只能获取一次的问题
- request.getInputStream()只能获取一次的问题
- request.getinputstream只能读取一次
- spring-session导致request.getInputStream流数据丢失问题
- 解决request.getParameter() 、 request.getInputStream()和request.getReader()只能使用一次的问题
- 使用request.getInputStream() 获取POST数据
- 使用request.getInputStream() 获取POST数据
- 使用request.getInputStream() 获取POST数据
- 使用request.getInputStream() 获取POST数据
- 面向过程 VS 面向对象
- 无法进入ubuntu怎么清理\boot旧版系统内核
- QGraphicsView框架事件处理
- C++学习(52)
- nginx源码学习(四)worker进程的启动
- Request.getInputStream()读取数据遇到的问题
- Retrofit用法详解
- shiro学习
- cocos2d-x 音乐播放
- 给DBGridEh的data group增加比值(如毛利)
- Core Foundation 对象的内存管理
- java类中执行脚本或命令(比如Linux下的脚本命令等)
- Android开发Gradle版本管理
- 关于URL+method、通用参数封装的设计思路(java web,SSM框架)