Request.getInputStream()读取数据遇到的问题

来源:互联网 发布:linux ftp server 编辑:程序博客网 时间:2024/06/06 03:33
  1. Servlet的Request.getInputStream()只能读取一次问题
          查看接口ServletRequest的源码
     publicServletInputStreamgetInputStream()throwsIOException;
     request.getInputStream()返回的其实是ServletInputStream。ServletInputStream继承了InputStream
    但是没有重写reset()方法。
    InputStreamreset()方法源码是这样的:
   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()两个方法只能被调用一次,而且不能两个都调用。

  1. /** 
  2.  * Retrieves the body of the request as binary data using 
  3.  * a {@link ServletInputStream}.  Either this method or  
  4.  * {@link #getReader} may be called to read the body, not both. 
  5.  * 
  6.  * @return          a {@link ServletInputStream} object containing 
  7.  *              the body of the request 
  8.  * 
  9.  * @exception IllegalStateException  if the {@link #getReader} method 
  10.  *                   has already been called for this request 
  11.  * 
  12.  * @exception IOException       if an input or output exception occurred 
  13.  * 
  14.  */  
  15.   
  16. public ServletInputStream getInputStream() throws IOException;   
  17.   
  18. /** 
  19.  * Retrieves the body of the request as character data using 
  20.  * a <code>BufferedReader</code>.  The reader translates the character 
  21.  * data according to the character encoding used on the body. 
  22.  * Either this method or {@link #getInputStream} may be called to read the 
  23.  * body, not both. 
  24.  *  
  25.  * 
  26.  * @return                  a <code>BufferedReader</code> 
  27.  *                      containing the body of the request   
  28.  * 
  29.  * @exception UnsupportedEncodingException  if the character set encoding 
  30.  *                      used is not supported and the  
  31.  *                      text cannot be decoded 
  32.  * 
  33.  * @exception IllegalStateException     if {@link #getInputStream} method 
  34.  *                      has been called on this request 
  35.  * 
  36.  * @exception IOException           if an input or output exception occurred 
  37.  * 
  38.  * @see                     #getInputStream 
  39.  * 
  40.  */  
  41.   
  42. public BufferedReader getReader() throws IOException;  
        两个方法都注明方法只能被调用一次,由于RequestBody是流的形式读取,那么流读了一次就没有了,所以只能被调用一次。既然是因为流只能读一次的原因,那么只要将流的内容保存下来,        就可以实现反复读取了。 

  4.解决 getInputStream读取多次的办法
     
     参考   http://liwx2000.iteye.com/blog/1542431
       
阅读全文
0 0