3 回答

TA贡献1852条经验 获得超7个赞
您可以使用org.apache.commons.io.IOUtils.copy将InputStream的内容复制到字节数组,然后使用ByteArrayInputStream从字节数组重复读取。例如:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
org.apache.commons.io.IOUtils.copy(in, baos);
byte[] bytes = baos.toByteArray();
// either
while (needToReadAgain) {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
yourReadMethodHere(bais);
}
// or
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
while (needToReadAgain) {
bais.reset();
yourReadMethodHere(bais);
}

TA贡献1820条经验 获得超10个赞
根据InputStream的来源,您可能无法重置它。您可以使用检查mark()和reset()支持markSupported()。
如果是这样,则可以调用reset()InputStream返回到开头。如果不是,则需要再次从源读取InputStream。
添加回答
举报