post请求体内容无法重复获取 为什么会无法重复读取呢?
以tomcat为例,在进行请求体读取时实际底层调用的是org.apache.catalina.connector.Request的getInputStream()方法,而该方法返回的是CoyoteInputStream输入流
1 2 3 4 5 6 7 8 9 10 11 12 13 public ServletInputStream getInputStream () throws IOException { if (usingReader) { throw new IllegalStateException(sm.getString("coyoteRequest.getInputStream.ise" )); } usingInputStream = true ; if (inputStream == null ) { inputStream = new CoyoteInputStream(inputBuffer); } return inputStream; }
在使用CoyoteInputStream进行读取时
1 2 3 4 5 6 7 8 9 10 11 12 13 public int read (byte [] b, int off, int len) throws IOException { if (closed) { throw new IOException(sm.getString("inputBuffer.streamClosed" )); } if (checkByteBufferEof()) { return -1 ; } int n = Math.min(len, bb.remaining()); bb.get(b, off, n); return n; }
而流读取完毕都会进行close,这个流close之后,close状态就置为了true,所以导致流无法进行二次读取