获取文件大小 - Apache Commons文件上传 [英] Getting File Size - Apache Commons File Upload
问题描述
我正在使用Servlet和JSP构建一个Web应用程序。我已经建立了一个文件上传功能使用Apache公共库。我想限制用户上传一个巨大的文件 - 因此设置为20 MB的限制。
我正在使用 Fileitem.getSize()
方法获取大小,我猜是读取整个文件以确定尺寸。如果用户尝试上传大小为几GB的文件,那么是否会在应用程序服务器上增加很多开销?有没有更好的选择获取文件大小?
如果您设置 sizeMax $
ServletFileUpload
上的c $ c>属性,那么只要超过一定数量的数据,请求就会被拒绝。
另外,如果使用 DiskFileItemFactory
,则可以设置一个阈值,以便将超过一定大小的文件流式传输到本地磁盘而不是保存在内存中,这可以防止大文件。
I am having a webapplication built using Servlet and JSP. I have built a file upload functionality using apache commons library. I would like to restrict the user from uploading a huge file - hence have set the limit as 20 MB.
I am getting the size using Fileitem.getSize()
method which I guess is reading the entire file to determine the size. If a user tries to upload a file of few GBs in size , then wouldn't it add a lot of overhead on the application server? Is there any better alternative for getting the file size?
If you set the sizeMax
property on ServletFileUpload
, then the request will be rejected as soon as more than a certain amount of data is read.
Also, if you use DiskFileItemFactory
, then you can set a threshold so that files over a certain size are streamed to local disk rather than being held in memory, which protects against large files.
这篇关于获取文件大小 - Apache Commons文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!