File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Track download completion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Track download completion" Watch "Track download completion" New topic

Track download completion

Tridib Samanta
Ranch Hand

Joined: Apr 28, 2004
Posts: 128
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 ]

Thanks, Tridib
SCJP 1.4, SCWCD 1.4, SCBCD 5.0
Charles Lyons
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
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.

On the other hand, you wouldn't be able to dynamically do anything with presenting pages using only server-side coding - you'd need to think about using client-side scripting like JavaScript to monitor the download progress, though I'm not sure how that would work since JavaScript can't access the local filesystem (to measure file size/write from stream to disk).

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 / Amazon Amazon UK )
Tridib Samanta
Ranch Hand

Joined: Apr 28, 2004
Posts: 128
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.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

As Charles Lyons implied, the only way to ensure that the client has finished downloading a file is to have something running on the client to report that completion.

And as he suggested, Javascript is unlikely to work because of the security features that prevent it from accessing files. So what other client-side technologies could you use?

Since you're posting here, you are probably familiar with Java. Java running in a browser? That must be an applet. You would have to sign it to allow it to access files on the client system.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63839

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.

[Asking smart questions] [About Bear] [Books by Bear]
Charles Lyons
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
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 "":

You'll also need to be familiar with 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?
Tridib Samanta
Ranch Hand

Joined: Apr 28, 2004
Posts: 128
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.

Thanks again everybody.
I agree. Here's the link:
subject: Track download completion
It's not a secret anymore!