Reputation: 1
I am using Apache HttpClient 4.5.1 to download a ".tgz" file and I am running my client program on Windows.
When I run, I get the file downloaded with a size 4,423,680 (and it can't open because of the wrong size). But the actual size of the file is 4,414,136 (using "wget").
The problem goes away when I use DefaultHttpClient (deprecated) instead of CloseableHttpClient (all other code being the same).
Code that created the problem:
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
CloseableHttpClient httpClient = HttpClients.createDefault();
(or)
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// the following code is the same in both cases
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet);
try {
BufferedInputStream bis = new BufferedInputStream(entity.getContent());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath)));
int inByte;
while ((inByte = bis.read()) != -1 ) {
bos.write(inByte);
}
}
....
Code that resolved the problem:
import org.apache.http.impl.client.DefaultHttpClient;
...
HttpClient httpClient = new DefaultHttpClient();
... <same code>....
DefaultHttpClient is deprecated, but it seems to work fine.
Don't understand what is wrong with the CloseableHttpClient.
Upvotes: 0
Views: 2315
Reputation: 27538
Please try out this code snippet and see if you still get a different file size.
CloseableHttpClient httpClient = HttpClientBuilder.create()
.disableContentCompression()
.build();
HttpGet get = new HttpGet(url);
try (CloseableHttpResponse response = httpClient.execute(get)) {
HttpEntity entity = response.getEntity();
if (entity != null) {
try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
entity.writeTo(outputStream);
}
}
}
Upvotes: 1
Reputation: 1320
I would like to give you several tips.
Fist, I think that you need to close the response, so use a try-with-resources block or add a finally block to the try-catch block
try(HttpResponse response = httpClient.execute(httpGet)) {
....
or
finally
{
response.close();
}
Maybe you already closed it and you didn't copy it into the sample. Please, forgive me if it is the case.
Second. Every time I got a similar behaviour was because the output stream was not flushed and closed. I mean that you need to flush and close the bos stream at the end of your code.
Final note: This was just a comment. Maybe that does not solve your issue. I am sorry but I can't understand why the usage of the other Http Client does not reproduce the same issue.
Upvotes: 1