Re: URL Connection & Files

From: John C. Bollinger (jobollin_at_indiana.edu)
Date: 03/25/04


Date: Thu, 25 Mar 2004 08:42:04 -0500

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 seek 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



Relevant Pages

  • Re: URL Connection & Files
    ... > having to download the file? ... If the HTTP HEAD request doesn't work then ... HTTP HEAD, and is less likely to be supported. ... The server must expose ...
    (comp.lang.java.help)
  • Re: HHTP file download:
    ... I simply sent a HEAD request to the server to get the content length before ... > give you a idea of how long its gonna take you to download the file. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: HHTP file download:
    ... I simply sent a HEAD request to the server to get the content length before ... > give you a idea of how long its gonna take you to download the file. ...
    (microsoft.public.dotnet.general)
  • Re: URL Connection & Files
    ... Mika Stenberg wrote: ... > having to download the file? ... Or if you don't have that locally, sending a HEAD request might work. ... depending on the server and the asset in question. ...
    (comp.lang.java.programmer)
  • Re: URL Connection & Files
    ... Mika Stenberg wrote: ... > having to download the file? ... Or if you don't have that locally, sending a HEAD request might work. ... depending on the server and the asset in question. ...
    (comp.lang.java.help)