For Programmers: Free Programming Magazines  


Home > Archive > Java Help > March 2004 > URL Connection & Files









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author URL Connection & Files
Mika Stenberg

2004-03-27, 12:30 am

Hi,

Am working on a small http - cache program &
Have some problems with Java & URL:s.

1) How can I check the file lastModified() - date through its URL, without
having to download the file?
I have tried using something like this, but it only works on some cases ( i
think it depends on the head).

HttpURLConnection connection = (HttpURLConnection)server.openConnection();
connection.setRequestMethod("HEAD") ;
System.out.println(connection.getLastModified());

2) Second question is about writing a file to some variable in memory, not
on HD. Note that the file may be either text or binary.
Which writer can I use and how? Can the data type still be File even if it
is stored in the memory?

Any ideas and suggestions are welcome!

Please reply to mika.stenberg@NO-SPAM-helsinki.fi
Cheers,
Mika




John C. Bollinger

2004-03-27, 12:30 am

Mika Stenberg wrote:

> Hi,
>
> Am working on a small http - cache program &
> Have some problems with Java & URL:s.
>
> 1) How can I check the file lastModified() - date through its URL, without
> having to download the file?
> I have tried using something like this, but it only works on some cases ( i
> think it depends on the head).
>
> HttpURLConnection connection = (HttpURLConnection)server.openConnection();
> connection.setRequestMethod("HEAD") ;
> System.out.println(connection.getLastModified());


For an HTTP URL (or any other remote URL) you cannot determine the
information you s without the cooperation of the host where the file
lives. For HTTP specifically, a HEAD request of the kind you show is a
reasonable attempt. I wouldn't assume that it would work every URLs
that designates a static file, however, as the server might or might not
choose to provide the information you want, and might or might not lie
to you if it did provide it. If the HTTP HEAD request doesn't work then
a GET ought not to work either; whether or not you retrieve the whole
file is not relevant.

Any other technique conceivable suffers from the same problems as the
HTTP HEAD, and is less likely to be supported. The server must expose
the information to you via some established protocol, and you have
little choice but to trust it.

> 2) Second question is about writing a file to some variable in memory, not
> on HD. Note that the file may be either text or binary.
> Which writer can I use and how? Can the data type still be File even if it
> is stored in the memory?


Try StringWriter for character data or ByteArrayOutputStream for binary
data.


John Bollinger
jobollin@indiana.edu

Jon A. Cruz

2004-03-27, 12:30 am

Mika Stenberg wrote:
> Hi,
>
> Am working on a small http - cache program &
> Have some problems with Java & URL:s.
>
> 1) How can I check the file lastModified() - date through its URL, without
> having to download the file?
> I have tried using something like this, but it only works on some cases ( i
> think it depends on the head).


I'd need to take a p at the Java classes to refresh my memory, but in
HTTP in general the means is to add an "If-Modified-Since" line in the
header.

Or if you don't have that locally, sending a HEAD request might work.
Might not, depending on the server and the asset in question.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com