InputStream在Apache FileUpload API中关闭 [英] InputStream closed in Apache FileUpload API

查看:200
本文介绍了InputStream在Apache FileUpload API中关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常具体的问题。我希望有人在这里有很好的FileUpload API知识。



如果你已经使用过这个API,你应该知道,当一个文件的最大大小超过接下来的项目不能被读取,因为FileSizeLimitExceededException被抛出,如果你试图再一次调用 hasNext()方法迭代到下一个项目,IOException将被抛出,因为输入流已关闭,您尝试再次阅读。



这种情况非常糟糕,因为下一个参数无法读取。修改此表单:

 < form action =...method =postenctype =multipart / form-数据> 
< input type =hiddenname =param1value =foo/>
< input type =filename =myFile/>< br />
< input type =hiddenname =param2value =bar/>
< input type =submit/>
< / form>

现在文件超过了最大大小。结果:读取
param1。
myFile生成FileSizeLimitExceededException。
param2不被读取。 / p>

我试图重写API的一部分 - 没有运气,因为我想读取下一个项目(本例中为param2)。



任何人都有同样的问题?如何修复它?



谢谢。




<编辑:那么,我已经实施了由BalusC提出的解决方案。未来:

FileUpload包装

FileUpload包装器的使用方法



如果你认为这个包装器需要任何东西只是说出来。我不接受它作为一个真正的解决方案,因为理想的是修改和修复API本身。 解决方案

黑客的API将设置上传文件的大小限制为无限,然后由 FileItem#getSize() 。这可能只会花费更长的时间才能让最终用户得到有关这方面的反馈,因为整个文件需要先读完(但是为了得到所有后续项目,您还是需要这样做)。



作为一个完全不同的选择,您可能需要查看新的 HTML5 File API ,它提供了一种在实际发送请求之前使用JavaScript检查文件大小的方法。

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


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;
// ...

这篇关于InputStream在Apache FileUpload API中关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆