全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

浅谈request.getinputstream只能读取一次的问题

首先,我们复习一下InputStream read方法的基础知识,

java InputStream read方法内部有一个,postion,标志当前流读取到的位置,每读取一次,位置就会移动一次,如果读到最后,InputStream.read方法会返回-1,标志已经读取完了,如果想再次读取,可以调用inputstream.reset方法,position就会移动到上次调用mark的位置,mark默认是0,所以就能从头再读了。

当然,能否reset是有条件的,它取决于markSupported,markSupported() 方法返回是否可以mark/reset

我们再回头看request.getInputStream

request.getInputStream返回的值是ServletInputStream,查看ServletInputStream源码发现,没有重写reset方法,所以查看InputStream源码发现marksupported 返回false,并且reset方法,直接抛出异常。

InputStream.java

 public boolean markSupported() {
    return false;
  }

 public synchronized void reset() throws IOException {
    throw new IOException("mark/reset not supported");
  }

综上所述,在request.getinputstream读取一次后position到了文件末尾,第二次就读取不到数据,由于无法reset(),所以,request.getinputstream只能读取一次。

总结:

这个问题最根本还是对java IO的read、reset方法的深入理解,尤其是read方法的内部实现原理。

附ServletInputStream.java源码

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package javax.servlet;

import java.io.IOException;
import java.io.InputStream;

/**
 * Provides an input stream for reading binary data from a client request,
 * including an efficient <code>readLine</code> method for reading data one line
 * at a time. With some protocols, such as HTTP POST and PUT, a
 * <code>ServletInputStream</code> object can be used to read data sent from the
 * client.
 * <p>
 * A <code>ServletInputStream</code> object is normally retrieved via the
 * {@link ServletRequest#getInputStream} method.
 * <p>
 * This is an abstract class that a servlet container implements. Subclasses of
 * this class must implement the <code>java.io.InputStream.read()</code> method.
 *
 * @see ServletRequest
 */
public abstract class ServletInputStream extends InputStream {

  /**
   * Does nothing, because this is an abstract class.
   */
  protected ServletInputStream() {
    // NOOP
  }

  /**
   * Reads the input stream, one line at a time. Starting at an offset, reads
   * bytes into an array, until it reads a certain number of bytes or reaches
   * a newline character, which it reads into the array as well.
   * <p>
   * This method returns -1 if it reaches the end of the input stream before
   * reading the maximum number of bytes.
   *
   * @param b
   *      an array of bytes into which data is read
   * @param off
   *      an integer specifying the character at which this method
   *      begins reading
   * @param len
   *      an integer specifying the maximum number of bytes to read
   * @return an integer specifying the actual number of bytes read, or -1 if
   *     the end of the stream is reached
   * @exception IOException
   *        if an input or output exception has occurred
   */
  public int readLine(byte[] b, int off, int len) throws IOException {

    if (len <= 0) {
      return 0;
    }
    int count = 0, c;

    while ((c = read()) != -1) {
      b[off++] = (byte) c;
      count++;
      if (c == '\n' || count == len) {
        break;
      }
    }
    return count > 0 ? count : -1;
  }

  /**
   * Returns <code>true</code> if all the data has been read from the stream,
   * else <code>false</code>.
   *
   * @since Servlet 3.1
   */
  public abstract boolean isFinished();

  /**
   * Returns <code>true</code> if data can be read without blocking, else
   * <code>false</code>. If this method is called and returns false, the
   * container will invoke {@link ReadListener#onDataAvailable()} when data is
   * available.
   *
   * @since Servlet 3.1
   */
  public abstract boolean isReady();

  /**
   * Sets the {@link ReadListener} for this {@link ServletInputStream} and
   * thereby switches to non-blocking IO. It is only valid to switch to
   * non-blocking IO within async processing or HTTP upgrade processing.
   *
   * @param listener The non-blocking IO read listener
   *
   * @throws IllegalStateException  If this method is called if neither
   *                 async nor HTTP upgrade is in progress or
   *                 if the {@link ReadListener} has already
   *                 been set
   * @throws NullPointerException   If listener is null
   *
   * @since Servlet 3.1
   */
  public abstract void setReadListener(ReadListener listener);
}

以上这篇浅谈request.getinputstream只能读取一次的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。


# request  # getinputstream  # 解决spring 处理request.getInputStream()输入流只能读取一次问题  # 为了多次读取ServletInputStream引发的一系列问题  # 完美解决request请求流只能读取一次的问题  # 如何构建可重复读取inputStream的request  # 就会  # 给大家  # 是有  # 尤其是  # 就能  # 这个问题  # 希望能  # 重写  # 这篇  # 浅谈  # 抛出  # 小编  # 读到  # 想再  # 大家多多  # 最根本  # 再回  # 有一个  # 再读  # 法会 


相关文章: 合肥做个网站多少钱,合肥本地有没有比较靠谱的交友平台?  个人网站制作流程图片大全,个人网站如何注销?  如何在建站宝盒中设置产品搜索功能?  如何在宝塔面板中创建新站点?  网站制作大概多少钱一个,做一个平台网站大概多少钱?  定制建站流程步骤详解:一站式方案设计与开发指南  制作ppt免费网站有哪些,有哪些比较好的ppt模板下载网站?  西安市网站制作公司,哪个相亲网站比较好?西安比较好的相亲网站?  广州网站制作公司哪家好一点,广州欧莱雅百库网络科技有限公司官网?  如何配置WinSCP新建站点的密钥验证步骤?  北京网站制作网页,网站升级改版需要多久?  关于BootStrap modal 在IOS9中不能弹出的解决方法(IOS 9 bootstrap modal ios 9 noticework)  如何在建站之星绑定自定义域名?  建站之星手机一键生成:多端自适应+小程序开发快速建站指南  香港服务器WordPress建站指南:SEO优化与高效部署策略  如何高效配置IIS服务器搭建网站?  专业网站建设制作报价,网页设计制作要考什么证?  佛山企业网站制作公司有哪些,沟通100网上服务官网?  如何在IIS中新建站点并配置端口与IP地址?  高防服务器租用如何选择配置与防御等级?  电视网站制作tvbox接口,云海电视怎样自定义添加电视源?  网站制作话术技巧,网站推广做的好怎么话术?  电影网站制作价格表,那些提供免费电影的网站,他们是怎么盈利的?  如何快速生成高效建站系统源代码?  红河网站制作公司,红河事业单位身份证如何上传?  建站主机空间推荐 高性价比配置与快速部署方案解析  如何选购建站域名与空间?自助平台全解析  建站之星导航如何优化提升用户体验?  网站设计制作企业有哪些,抖音官网主页怎么设置?  如何用景安虚拟主机手机版绑定域名建站?  如何在万网自助建站平台快速创建网站?  东莞专业制作网站的公司,东莞大学生网的网址是什么?  郑州企业网站制作公司,郑州招聘网站有哪些?  网站专业制作公司,网站编辑是做什么的?好做吗?工作前景如何?  建站主机服务器选型指南与性能优化方案解析  沈阳制作网站公司排名,沈阳装饰协会官方网站?  我的世界制作壁纸网站下载,手机怎么换我的世界壁纸?  早安海报制作网站推荐大全,企业早安海报怎么每天更换?  h5网站制作工具有哪些,h5页面制作工具有哪些?  如何快速搭建二级域名独立网站?  怎么用手机制作网站链接,dw怎么把手机适应页面变成网页?  如何制作一个表白网站视频,关于勇敢表白的小标题?  企业网站制作费用多少,企业网站空间一般需要多大,费用是多少?  如何用PHP工具快速搭建高效网站?  公司网站制作需要多少钱,找人做公司网站需要多少钱?  南平网站制作公司,2025年南平市事业单位报名时间?  建站之星伪静态规则如何正确配置?  详解jQuery中基本的动画方法  简单实现Android验证码  详解jQuery停止动画——stop()方法的使用 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。