开发者

InputStream closed in Apache FileUpload API

开发者 https://www.devze.com 2023-04-06 01:48 出处:网络
This is a very specific question. I hope there\'s someone here with good knowledge in FileUpload API.

This is a very specific question. I hope there's someone here with good knowledge in FileUpload API.

If you have been worked with this API you should know that when the max size of a file has been exceeded the next items cannot be read because the FileSizeLimitExceededException is thrown and if you attempt to call one more time to hasNext() method to iterate to the next item an IOException is thrown because an inputstream is closed and you attempt to read it again.

This scenario is very bad because the next parameters cannot be read. Supose this form:

<form action="..." method="post" enctype="multipart/form-data">
    <input type="hidden" name="param1" value="foo"/>
    <input type="file" name="myFile"/><br/>
    <input type="hidden" name="param2" value="bar"/>
    <input type="submit"/>
</form>

Now the file exceeds the maximum size. Result:

"param1" is read.

"myFile" generates FileSizeLimitExceededException.

"param2" is not read.

I'm trying to rewrite part of the API -with no luck- because I want to read the next items ("param2" in this case).

Anyone has had the same 开发者_运维问答problem? How have you fixed it?

Thanks.


EDIT: Well, I've implemented the solution proposed by BalusC. For the future:

FileUpload wrapper

FileUpload wrapper usage

If you consider that this wrapper needs anything else just say it. I don't accept it as a real solution because the ideal is to modify and fix the API itself.


A way without hacking the API would be so set the upload file size limit to "unlimited" and examine the upload file size yourself afterwards by FileItem#getSize(). This may only take longer before the enduser get feedback about that, because the entire file needs to be read fully first (but you need to do it anyway in order to get all subsequent items).

As a completely different alternative, you may want to look at the new HTML5 File API which offers you a way to examine the file size using JavaScript before the request is actually been sent.

var size = document.getElementById("fileFieldId").files[0].size;
// ...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号