主頁 > 知識(shí)庫 > 淺談request.getinputstream只能讀取一次的問題

淺談request.getinputstream只能讀取一次的問題

熱門標(biāo)簽:好操作的電話機(jī)器人廠家 企業(yè)怎么在聯(lián)通申請(qǐng)400電話 如何用中國(guó)地圖標(biāo)注數(shù)字點(diǎn) 聊城智能電銷機(jī)器人外呼 南昌市地圖標(biāo)注app 泰州泰興400電話 怎么申請(qǐng) 百度地圖添加標(biāo)注圖標(biāo)樣式 南京新思維電話機(jī)器人 地圖標(biāo)注市場(chǎng)怎么樣

首先,我們復(fù)習(xí)一下InputStream read方法的基礎(chǔ)知識(shí),

java InputStream read方法內(nèi)部有一個(gè),postion,標(biāo)志當(dāng)前流讀取到的位置,每讀取一次,位置就會(huì)移動(dòng)一次,如果讀到最后,InputStream.read方法會(huì)返回-1,標(biāo)志已經(jīng)讀取完了,如果想再次讀取,可以調(diào)用inputstream.reset方法,position就會(huì)移動(dòng)到上次調(diào)用mark的位置,mark默認(rèn)是0,所以就能從頭再讀了。

當(dāng)然,能否reset是有條件的,它取決于markSupported,markSupported() 方法返回是否可以mark/reset

我們?cè)倩仡^看request.getInputStream

request.getInputStream返回的值是ServletInputStream,查看ServletInputStream源碼發(fā)現(xiàn),沒有重寫reset方法,所以查看InputStream源碼發(fā)現(xiàn)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到了文件末尾,第二次就讀取不到數(shù)據(jù),由于無法reset(),所以,request.getinputstream只能讀取一次。

總結(jié):

這個(gè)問題最根本還是對(duì)java IO的read、reset方法的深入理解,尤其是read方法的內(nèi)部實(shí)現(xiàn)原理。

附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只能讀取一次的問題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

標(biāo)簽:白銀 開封 山南 吉林 烏蘭察布 臨汾 自貢 銅川

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《淺談request.getinputstream只能讀取一次的問題》,本文關(guān)鍵詞  淺談,request.getinputstream,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《淺談request.getinputstream只能讀取一次的問題》相關(guān)的同類信息!
  • 本頁收集關(guān)于淺談request.getinputstream只能讀取一次的問題的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章