Hi All, I have a servlet which is used for file download. I want to make sure the user completed the download and depending on that I will update the download status as "DOWNLOADED". If the download aborts in the middle or he/she clicks "Cancel" button on the download popup, the download status will be "NOT_DOWNLOADED". How can I do this? This is a my current project related issue and I am at a doomed condition. Guys I really need your help! [ May 24, 2007: Message edited by: Tridib Samanta ]
You can't do this through the servlet model because the container abstracts away all the HTTP and TCP so you don't have to worry about the protocols; of course, on the flip side, it does mean you don't have total control.
Ultimately, you can track download progress if you know the amount transferred (e.g. from a file size) and the total to transfer (from the HTTP response headers), but you will neither find this information through servlets, nor through most HTTP implementations (since the data written goes into a TCP buffer, and the amount delivered from the buffer isn't guaranteed because the connection can be closed at any time).
Why do you need to do this, and is there no alternative? Is there a website that already achieves so you could use their client-side scripting as a starting point?
Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / AmazonAmazon UK )
Joined: Apr 28, 2004
Thanks Charles! I got your point. The requirement is like this: These files contain very confidential data. We bring the file from a different environment to our local system. As soon as the user download it, we have to remove it from our local system. But the file should be available for download to the user if the user fails to download the file at first attempt or so. Could you please suggest any other alternative solution? Thanks in adavance.
Are your users trusted? If so, you could do something as simple as have them let you knw when the download completes.
The USPS uses this technique for their Clik-n-ship facility. After the postage has been downloaded and printed, the web app asks "did it print correctly?". You could do the same sort of thing assuming that you are trusting your users to tell the truth.
If not, then something along the lines of what Paul suggests may be your only option.
You could try an applet, though you'll need to sign it with an appropriate certificate. Then the user will be presented with a dialog asking them if they trust the applet: if not, they won't be able to download the file. If so, the download can progress (you'll probably want to put a progress bar in your applet). Note that this isn't perfect - there are problems on some platforms such as Vista:
The disadvantage with the applet is: (1) you've got to have all clients supporting your applet + JRE version (this is a real bind); (2) the server still can't determine when the transfer's complete except if your applet signals it somehow (i.e. issues another 'special' HTTP request) after transfer.
Second thought: do you really need to cache it locally at all? Couldn't you configure a proxy so when the client makes an HTTP request to your server, it sends a request to the remote server for the file, and all data returned is buffered to the client? That way you never actually store the file yourself at all. You can implement different security mechanisms at each stage with the proxy if necessary. You can probably find suitable software available (e.g. Apache's modules), or build your own with the Java 6 HTTP server (see below).
Finally, if you really must cache, you could build a second Java application on the server which implements HTTP directly over TCP, and track the underlying connection that way - once the TCP connection (socket) closes or all the data was written, delete the file. This also means your program can monitor the file's last modified date (when it was downloaded to your server), and if it's too far in the past, delete it anyway (assuming the client isn't going to download it - and if they are, they can just go back through the Web page again).
For this last solution, take a look at the new Java 6 HTTP server in packages "com.sun.net.httpserver":
You'll also need to be familiar with java.net to get it working, but by all accounts it shouldn't be too difficult. Your only problem then is clients with firewalls which block non-standard ports, since you'll have to run your program as a daemon on another port.
Does that help?
Joined: Apr 28, 2004
Thanks everybody for your great replies. I think using applet is more appropriate for my project requirement. As, Bear suggested, I also thought to let the user confirm the download. But after download, they may skip this step and the documents may be still available. Thanks Charles for your suggested solutions. But it seems very tough and I don't have sufficient knowledge to implement those. I must give a try later.