aspose file tools*
The moose likes Servlets and the fly likes Cache file(pdf) in user's computer using Servlet-Cookie, or Session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Cache file(pdf) in user Watch "Cache file(pdf) in user New topic
Author

Cache file(pdf) in user's computer using Servlet-Cookie, or Session

Sam Gehouse
Ranch Hand

Joined: Jul 21, 2003
Posts: 281
How can I save a file (pdf) in the cache of a browser (IE 6) in a user's computer? I am using Servlet and Tomcat 4.1.24. PDF file is sitting in server (remote computer) under a URL space.
The idea is to NOT to download the pdf file on user's computer (from the remote server) if the user has a cached copy of the pdf file and pdf file in the server is the same as the pdf file in the browser. If cached pdf is different from pdf in browser, or, if the user does not have that pdf file in cache, then download pdf file from the server.
Can I come up with a way to use Cookie class? Cookie class does not have an API which will let me save a File/Object in it. I could have saved the pdf file in the Cookie with 999999999 setMaxAge. Is storing that pdf file in session the only choice? I do not see session serving my purpose - session should not be valid for a longgg duration e.g. week or month.
Also, how would I create a file given a URL to that file.
URL fileInServer = new URL("http://www.myDomain.com/myWebApp/PDF/my.pdf");
URLConnection conn = fileInServer.openConnection();
Does getContent() help and what are the steps after that?
Any help with the source code will be appreciated.
Thanks,
Sam
sgho@aol.com
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Cookies are strictly limited to storing text, and not very much text at that. What a browser caches is very much up to the caching policy the user sets.
Are you generating this PDF file on the fly?
Do you want the PDF file to be served by a servlet or the normal web server?
Bill
Sam Gehouse
Ranch Hand

Joined: Jul 21, 2003
Posts: 281
I am NOT creating this PDF file on the fly. This pdf file is sitting in the server (remote machine) under URL e.g. http://10.10.10.xxx:8081/myApp/PDF/my.pdf
I need to check if this pdf file is in the cache in user's computer (differnt machine than the server) and if the cached pdf is the same as the pdf in the server. If that file is not in the cache, or if they are not the same, then, I need to save the pdf file from the server into user's browser cache.
I am writing all that code in Servlet. I can save the file in session. I do NOT know how else can I cache the file, other than saving it in session. I can not think of any setHeader() which will let me save a file in user's browser cache.
Also, how would I create a file, given a URL to that file.
URL fileInServer = new URL("http://www.myDomain.com/myWebApp/PDF/my.pdf");
URLConnection conn = fileInServer.openConnection();
Does getContent() help and what are the steps after that?
Thanks,
Sam
sgho@aol.com
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
I can't think of any way for you to determine if the file is already in the browser cache or to control whether or not the browser requests the file from the server. From the server side, you can only send a response, what the browser does with it is up to processes on the browser.
As far as figuring out a server file path from a URL, look at the getRealPath method of ServletContext.
Bill
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Wouldn't overriding the servlet's getLastModified() method solve this problem? The browser won't download the file again if the last-modify date hasn't changed since the last download.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Originally posted by Ron Newman:
Wouldn't overriding the servlet's getLastModified() method solve this problem? The browser won't download the file again if the last-modify date hasn't changed since the last download.

Then I think pdf file cant be accessed directly like domainname/file.pdf
It has to route through servlet.
and i dont think they are cached.


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cache file(pdf) in user's computer using Servlet-Cookie, or Session