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.