Re: URL Connection & Files
From: John C. Bollinger (jobollin_at_indiana.edu)
Date: 03/25/04
- Next message: William Brogden: "Re: How do I create a java based HTTPRequestHandler???"
- Previous message: Chris Uppal: "Re: Semi OT: CRM System in Java"
- In reply to: Mika Stenberg: "URL Connection & Files"
- Next in thread: Jon A. Cruz: "Re: URL Connection & Files"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: William Brogden: "Re: How do I create a java based HTTPRequestHandler???"
- Previous message: Chris Uppal: "Re: Semi OT: CRM System in Java"
- In reply to: Mika Stenberg: "URL Connection & Files"
- Next in thread: Jon A. Cruz: "Re: URL Connection & Files"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|