user11105060 wrote:You could keep track of the file in the user's session. Then you have two things:
Now I want to either have a button on this new html page for user to download file, and the file to be deleted from disk regardless. Or automatically stream the file when the new html page containing information about the file is displayed
Therefore it leaves a window open for someone to upload multiple files to the server within 30 minutes and not stream the download, resulting in the servers hard drive filling up very quickly.So? Is this allowed or not? If not: block it in your code. You are already binding files to the user's session, if he tries to upload a second one without doing something with the first, either block it or ask if the previous one should be deleted before processing the next one.
I thought of tracking the size of this folder and automatically clearing it if it gets above a certain size.You could do that. I would create a background service for that though; for example you could use Quartz to make it a scheduled job. When it is not tied to an action a user triggers through the web, you're safe. I wouldn't clear it though as you'll remove files that have been recently uploaded and will still be used, I would remove files that are of a certain age.
Also, I am concerned about temporary files created by the servlet which are not automatically deleted in situations when the server is closed unexpected.Again, ain't gonna happen. And in the off-chance it does happen SOME TIME, a cleanup job scheduled to run once at server startup (again, quartz is very good for this) can certainly fix it.