Skip to content

Commit 3ffb735

Browse files
committed
added download file with OkHttp
1 parent acf6683 commit 3ffb735

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed

okhttp-examples/.classpath

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
10+
<attributes>
11+
<attribute name="optional" value="true"/>
12+
<attribute name="maven.pomderived" value="true"/>
13+
</attributes>
14+
</classpathentry>
15+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
16+
<attributes>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="output" path="target/classes"/>
26+
</classpath>

okhttp-examples/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

okhttp-examples/.project

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>okhttp-examples</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>

okhttp-examples/pom.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.howtoprogram.okhttp</groupId>
6+
<artifactId>okhttp-examples</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>OkHttp Examples and Tutorials</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>com.squareup.okhttp3</groupId>
20+
<artifactId>okhttp</artifactId>
21+
<version>3.4.1</version>
22+
</dependency>
23+
24+
</dependencies>
25+
</project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.howtoprogram.okhttp.download;
2+
3+
import java.io.FileOutputStream;
4+
import java.io.IOException;
5+
6+
import okhttp3.Call;
7+
import okhttp3.Callback;
8+
import okhttp3.OkHttpClient;
9+
import okhttp3.Request;
10+
import okhttp3.Response;
11+
12+
public class DownloadServiceImplOkHttp {
13+
14+
public void downloadFileSync(String downloadUrl) throws Exception {
15+
OkHttpClient client = new OkHttpClient();
16+
Request request = new Request.Builder().url(downloadUrl).build();
17+
18+
Response response = client.newCall(request).execute();
19+
if (!response.isSuccessful()) {
20+
throw new IOException("Failed to download file: " + response);
21+
}
22+
FileOutputStream fos = new FileOutputStream("d:/tmp.txt");
23+
fos.write(response.body().bytes());
24+
fos.close();
25+
}
26+
27+
public void downloadFileAsync(final String downloadUrl) throws Exception {
28+
OkHttpClient client = new OkHttpClient();
29+
Request request = new Request.Builder().url(downloadUrl).build();
30+
client.newCall(request).enqueue(new Callback() {
31+
public void onFailure(Call call, IOException e) {
32+
e.printStackTrace();
33+
}
34+
35+
public void onResponse(Call call, Response response) throws IOException {
36+
if (!response.isSuccessful()) {
37+
throw new IOException("Failed to download file: " + response);
38+
}
39+
FileOutputStream fos = new FileOutputStream("d:/tmp.txt");
40+
fos.write(response.body().bytes());
41+
fos.close();
42+
}
43+
});
44+
}
45+
}

0 commit comments

Comments
 (0)