I have a servlet application that need to receive some files from some clients (in general not html\javascript based!) and I have to limit the filesize for each file. Thisi is a classical problem.
The streaming API of the apache fileUpload allow to stream a multi-part request to the server avoiding the need of saving the request content in temporary files before they are processed. Is this Correct?
Anyway with this approach I obviously can't know the actual filesize.
What I though was:
- The client knows the file size. So it will send a form-field containing the file size.
- The content-length http header contains the request size, so I can take it as an upperbound of the filesize
- I can count the bytes I'm saving
Now assuming that I want to make all the necessary validations server-side. This because I'm paranoic and I don't trust the clients, then:
a. options (1) and (2) are useful in case "good" clients make bad requests. So a first validation can be based on that.
b. option (3) is the only completely server-side option I found. So it is needed.
So I though that I could count the bytes I read and if the file exceedes the size-limit I print errors, delete the file I was writing and then make the "return" in servlet doPost. Am I doing right or there are some other better way to go?