Skip to content

Commit dbe22e1

Browse files
rajat-gargrajatgarg
and
rajatgarg
authored
[BAEL-8567] Add support for Downloading file using Apache HttpClient (#17641)
* [BAEL-8567] Add support for Downloading file using Apache HttpClient * [BAEL-8567] Update maven dependency --------- Co-authored-by: rajatgarg <rajatgarg@adobe.com>
1 parent a7ec13a commit dbe22e1

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

core-java-modules/core-java-networking-2/pom.xml

+6
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,19 @@
4545
<version>${greenmail.version}</version>
4646
<scope>test</scope>
4747
</dependency>
48+
<dependency>
49+
<groupId>org.apache.httpcomponents.client5</groupId>
50+
<artifactId>httpclient5</artifactId>
51+
<version>${apache.httpclient.version}</version>
52+
</dependency>
4853
</dependencies>
4954

5055
<build>
5156
<finalName>core-java-networking-2</finalName>
5257
</build>
5358

5459
<properties>
60+
<apache.httpclient.version>5.3.1</apache.httpclient.version>
5561
<angus.mail.version>2.0.1</angus.mail.version>
5662
<async-http-client.version>2.4.5</async-http-client.version>
5763
<jakarta.bind.version>2.3.3</jakarta.bind.version>

core-java-modules/core-java-networking-2/src/main/java/com/baeldung/download/FileDownload.java

+29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package com.baeldung.download;
22

33
import org.apache.commons.io.FileUtils;
4+
import org.apache.hc.client5.http.classic.methods.HttpGet;
5+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
6+
import org.apache.hc.client5.http.impl.classic.HttpClients;
7+
import org.apache.hc.core5.http.HttpEntity;
8+
import org.apache.hc.core5.http.io.entity.EntityUtils;
49
import org.asynchttpclient.*;
510

611
import java.io.*;
@@ -89,4 +94,28 @@ public FileOutputStream onCompleted(Response response) throws Exception {
8994
client.close();
9095
}
9196

97+
public static void downloadWithApacheHttpClient(String url, String localFileName) throws IOException {
98+
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
99+
HttpGet httpGet = new HttpGet(url);
100+
httpClient.execute(httpGet, classicHttpResponse -> {
101+
int code = classicHttpResponse.getCode();
102+
if (code == 200) {
103+
HttpEntity entity = classicHttpResponse.getEntity();
104+
if (entity != null) {
105+
try (InputStream inputStream = entity.getContent();
106+
FileOutputStream fileOutputStream = new FileOutputStream(localFileName)) {
107+
byte[] dataBuffer = new byte[1024];
108+
int bytesRead;
109+
while((bytesRead = inputStream.read(dataBuffer)) != -1) {
110+
fileOutputStream.write(dataBuffer, 0, bytesRead);
111+
}
112+
}
113+
}
114+
EntityUtils.consume(entity);
115+
}
116+
return classicHttpResponse;
117+
});
118+
}
119+
}
120+
92121
}

core-java-modules/core-java-networking-2/src/test/java/com/baeldung/download/FileDownloadIntegrationTest.java

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ public void givenApacheCommonsIO_whenDownloadingFile_thenDownloadShouldBeCorrect
5656
FileDownload.downloadWithApacheCommons(FILE_URL, FILE_NAME);
5757
assertTrue(checkMd5Hash(FILE_NAME));
5858
}
59+
60+
@Test
61+
public void giveApacheHttpClient_whenDownloadingFile_thenDownloadShouldBeCorrect() throws IOException, NoSuchAlgorithmException {
62+
FileDownload.downloadWithApacheHttpClient(FILE_URL, FILE_NAME);
63+
assertTrue(checkMd5Hash(FILE_NAME));
64+
}
5965

6066
@Test
6167
public void givenJavaIO_whenDownloadingFileStops_thenDownloadShouldBeResumedCorrectly() throws NoSuchAlgorithmException, IOException, URISyntaxException {

0 commit comments

Comments
 (0)