[prev in list] [next in list] [prev in thread] [next in thread] 

List:       httpclient-users
Subject:    re:Downloading files with http client
From:       Roland Weber <ROLWEBER () de ! ibm ! com>
Date:       2005-02-02 15:12:13
Message-ID: OFDC1D60D5.93418A5A-ONC1256F9C.005220E2-C1256F9C.00536F4D () de ! ibm ! com
[Download RAW message or body]

--=_alternative 005321ACC1256F9C_=
Content-Type: text/plain; charset="US-ASCII"

Hello,

ergodic 7 <ergodic7@yahoo.com> wrote on 02.02.2005 15:32:21:

> Is there any way of finding out the size of a file
> before downloading it via the get method?
> 
> GetMethod method = new GetMethod(url);
> method.getResponseBody();
> 
If method.getResponseContentLength() returns something different
than -1, you have the length. But if the server doesn't send a
"Content-Length" header, the length information is just not
available.

Either way, if you expect large files to be sent, you should
*never* use method.getResponseBody(), because the response body
will then be read into a byte array in memory. Instead, use
method.getResponseBodyAsStream() to access the method body.
This will also allow you to read the body in digestible chunks
(like 16K at a time or so) and write it to a file on disk
without clogging the JVM heap.

That's if "downloading" refers to the act of reading the
response body. If you want to know the size before even
executing the GET request, you have to try the HEAD method
as suggested by Dominic.

hope that helps,
  Roland
--=_alternative 005321ACC1256F9C_=--

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic