InputStream closed in Apache FileUpload API

This is a very specific question. Hopefully here someone here is very familiar with the FileUpload API.

If you are working with this API, you should be aware that when the maximum file size is exceeded, the following items cannot be read because FileSizeLimitExceededException is thrown, and if you try to call the hasNext() method hasNext() to iterate to the next element, an IOException is thrown. because the input stream is closed and you are trying to read it again.

This scenario is very bad because the following parameters cannot be read. Add 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:
reads "param1".
"myFile" throws a FileSizeLimitExceededException.
"param2" is not readable.

I am trying to rewrite part of the API - with no luck - because I want to read the following elements (in this case "param2").

Has anyone had the same problem? How did you fix it?

Thanks.


EDIT: Well, I implemented the solution offered by BalusC. For the future:
Fileupload wrapper
Using FileUpload Wrap

If you think this shell needs something else, just say it. I do not accept this as a real solution, because changing and fixing the API itself is ideal.

+2
source share
1 answer

In the method without hacking the API, the maximum size limit for the downloaded file is set to "unlimited" and then analyze the size of the downloaded file FileItem#getSize() . This may take longer before the end user receives feedback about this, because the entire file must first be read completely (but you still need to do everything to get all the subsequent elements).

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

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

Source: https://habr.com/ru/post/897464/


All Articles