English 中文(简体)
How to limit upload file size in Wicket
原标题:

How to limit file size in uploads in Apache Wicket version 1.4?

I am using FileUploadField to handle upload with normal form submit without any Ajax stuff. Is it enough to use Form.setMaxSize() to limit the size of uploaded file?

If too large file is uploaded, the browser will upload the whole file and Wicket will create validation error message with key [form-id].uploadTooLarge.

But how Wicket internally handles this situation, creating temporary files etc?

I d like to prevent a case where user uploads file of several GBs that doesn t fit to memory or disk while Wicket handles the request.

最佳回答

I did some digging in the wicket repository and found that the file is actually written to disk by FileUploadBase.parseRequest(RequestContext ctx). This class checks the file size before writing any of it to disk.

The file size check ultimately uses javax.servlet.ServletRequest.getContentLength() to determine the size of the file, which means the actual implementation varies based on what servlet container you use; but, I d say it s safe to assume that anyone who has written a servlet implementation knew enough to get the file size from the header instead of writing the whole thing to disk and then checking its size. So, you do not have to worry about folks trying to upload huge files using up all your disk space.

问题回答

The documentation on Form says:

In case of an upload error two resource keys are available to specify error messages: uploadTooLarge and uploadFailed ie in [page].properties [form-id].uploadTooLarge=You have uploaded a file that is over the allowed limit of 2Mb

My guess is those get fired in form submit validation.. Have you tried to see if this is the case?

form.setMaxSize(Bytes.kilobytes(fileUploadMaximumKilobytes));




相关问题
How to set response filename without forcing "save as" dialog

I am returning a stream in some response setting the appropriate content-type header. The behavior I m looking for is this: If the browser is able to render content of the given content type then it ...

Which Http redirects status code to use?

friendfeed.com uses 302. bit.ly uses 301. I had decided to use 303. Do they behave differently in terms of support by browsers ?

Does HttpWebRequest send 200 OK automatically?

Background: I am implementing Paypal IPN handler. This great article on Paypal states that I am required to send a 200 OK back to Paypal after I read the response. The processing of IPN request is ...

Java HTTPAUTH

我试图把桌面应用程序连接起来,我是同D.icio.us api @ Delicious Alan书写的,简单地向他们提供我的用户名和密码,并请他把书记上写给我......。

Finding out where curl was redirected

I m using curl to make php send an http request to some website somewhere and have set CURLOPT_FOLLOWLOCATION to 1 so that it follows redirects. How then, can I find out where it was eventually ...

热门标签