使用commons-fileupload处理上传文件FileItemStream$ItemSkippedException

问题:org.apache.commons.fileupload.FileItemStream$ItemSkippedException

因为解析post request的时候把inputstream放在了集合中,打算事后统一处理,如下,这样就出现了这个问题。

1
2
3
4
5
6
7
8
9
10
11
12

while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();

if (item.isFormField()) {
fields.put(name, Streams.asString(stream));
} else {
streamList.add(stream);
}
}

官网给出的解释是:

This exception is thrown, if an attempt is made to read data from the InputStream, which has been returned by FileItemStream.openStream(), after Iterator.hasNext() has been invoked on the iterator, which created the FileItemStream.

最后我的解决方法是:把inputstream先转为bytearray,然后再转化为InputStream,如下。会不会太耗内存?

1
2
byte[] bytes = IOUtils.toByteArray(stream);
streamList.add(new ByteArrayInputStream(bytes));