Getting File Size - Apache Commons File Upload

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 08:29:05

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/8725375/getting-file-size-apache-commons-file-upload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!