counting how many times file is downloaded
Hi,
In my web application I need to set the limit say 10 times, a file can be downloaded by the user. After downloading 10 times I need to disable the download link. I am using javascript,
ajax and spring framework and mysql .
Problem is when the file is being downloaded if the user clicks on cancel button of browser download manager or if they closes the browser , download count is incremented though the file is not downloaded completely. I need to catch the events when the download is incomplete so that I should not increment the download count that time.
Is there any way to solve this problem?
Please help me.....
[662 byte] By [
nekara] at [2007-11-27 8:51:23]

I'm not sure if there's an easy way to do what you want to do.
You could try using a Servlet to serve the file though. What you'd basically need to do is set up a servlet to take a "file" parameter, load up the file, and then send it to the output stream. Once that was done you could increment the file download count.
So, in pseudo-code, what you'd be doing is:
doGet(HttpServletRequest request, HttpServletResponse response)
{
String filename = request.getParameter("filename");
File f = new File(FILE_DIR + filename);
if (downloadCount < MAX_DOWNLOAD_COUNT)
{
// TODO: Create FileInputStream and write contents to response.getOutputStream()
// If you're using Spring, looking into the FileCopyUtils class as this will make it much easier
// Increment download count
downloadCount++;
}
}
When someone clicks the "cancel" button on a download, you should find this closes the output stream, which will throw an exception and your download count will not be incremented. You might want to verify that though by testing it!
Of course, in the interests of security you would need to check that the filename being passed in didn't contain any special characters (particularly backslashes or forward slashes).
And I don't know how you're storing the download count, but of course you'd probably want to modify it so that it stored on a "per-file" basis!