0

我试图分析我的应用程序中最近发现的一个问题,并意识到我的inputStream.reset()方法失败了,因为我试图在FileInputStream.

似乎我的方法调用 For apache 的DiskFile.getInputStram()返回ByteArrayInputStream实例(标记支持)或FileInputStream(标记不支持)基于特定文件大小阈值的实例。

我必须得到这个输入流的代码是:

FormFile file = multipartForm.getFiles().get(0); // It's always one file
InputStream is = file.getInputStream();

// Read the stream and did job
// Now I want to reset it.
// bad coding from my side because I didn't check markSupported

is.reset();

// Got IO error immediately after this. But anything below 256KB is ok

我确信在 Oracle JDK 文档或 apache 的站点中某处提到/解释了这一点。但似乎不记得任何参考。有谁知道这种行为是否有意义?

4

1 回答 1

2

我不熟悉 Struts API,但对我来说,似乎当返回类型InputStream而不是特定子类时,您无法保证返回流的实际类型。

由于调用reset()仅在有前一个mark(readlimit)调用时才有效,因此通常处理未指定的InputStream类型是直截了当的:

InputStream inputStream = …
int readlimit = …

if(!inputStream.markSupported()) {
    inputStream = new BufferedInputStream(inputStream, readlimit);
}

inputStream.mark(readlimit);
// read some date, not more than readlimit
inputStream.reset();
于 2017-11-17T13:13:51.427 回答