3

I need to download and save file. I'm trying to use HTTPBuilder because it has simple API and supports cookies. I have written following code:

//create new httpBuilder and set cookies
def httpBuilder = ...
def file = ...  
def inputStream = httpBuilder.get(uri: urlData.url, contentType: ContentType.BINARY)
FileUtils.copyInputStreamToFile(inputStream)
  1. How can I check that file is correctly downloaded (not only the part of the file)?
  2. For large files exception java.lang.OutOfMemoryError: Java heap space occurs on line def inputStream = httpBuilder.get... How can I solve it?
  3. May be it's not best choise to download files by HTTPBuilder. What is the best way to download file with cookies support?
tim_yates
  • 167,322
  • 27
  • 342
  • 338
fedor.belov
  • 22,343
  • 26
  • 89
  • 134
  • I don't know much about httpbuilder, but it seems the stream is being loaded into memory, can you check that? – Will Nov 28 '12 at 14:07
  • Yep, looks like httpBuilder loads stream in memory in any case - if you are using clousure api or simple api as above. I have solved it with `httpBuilder.client.execute` – fedor.belov Nov 28 '12 at 16:18

1 Answers1

4

Have you tried HttpBuilder GET request with custom response-handling logic:

httpBuilder.get(uri: urlData.url, contentType: ContentType.BINARY) { 
   resp, inputStream ->
       FileUtils.copyInputStreamToFile(inputStream)
}

If HttpBuilder has problems which is strange then you can always use the tried and true Apache HttpClient API which has full cookie support.

HttpGet req = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(req);
// validate response code, etc.
InputStream inputStream = response.getEntity().getContent();

You can add a localContext when executing the request to manage cookies.

CodeMonkey
  • 22,825
  • 4
  • 35
  • 75