Hello, ergodic 7 <[EMAIL PROTECTED]> 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
