From 08b40786350ad870b79bb25fb686d720ba66125d Mon Sep 17 00:00:00 2001 From: chentianming Date: Thu, 7 Jan 2021 17:06:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0=E7=A4=BA?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- README.md | 55 ++++++++++++++++++- README_EN.md | 51 +++++++++++++++++ .../spring/boot/test/DownloadTest.java | 54 ++++++++++++++++++ .../spring/boot/test/http/DownloadApi.java | 19 +++++++ 5 files changed, 178 insertions(+), 3 deletions(-) create mode 100644 src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/DownloadTest.java create mode 100644 src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/DownloadApi.java diff --git a/.gitignore b/.gitignore index f6fcae9..b7570f3 100644 --- a/.gitignore +++ b/.gitignore @@ -6,5 +6,5 @@ .DS_Store .idea/ *.iml - +temp **/pom.xml.releaseBackup \ No newline at end of file diff --git a/README.md b/README.md index c97125f..f4f52ee 100644 --- a/README.md +++ b/README.md @@ -760,7 +760,7 @@ retrofit: ## 其他功能示例 -### 上传文件示例 +### 上传文件 #### 构建MultipartBody.Part @@ -781,7 +781,58 @@ Void upload(@Part MultipartBody.Part file); ``` -### 动态URL示例 +### 下载文件 + +#### http下载接口 + +```java +@RetrofitClient(baseUrl = "https://img.ljcdn.com/hc-picture/") +public interface DownloadApi { + + @GET("{fileKey}") + Response download(@Path("fileKey") String fileKey); +} + +``` + +#### http下载使用 + +@SpringBootTest(classes = RetrofitTestApplication.class) +@RunWith(SpringRunner.class) +public class DownloadTest { + + @Autowired + DownloadApi downLoadApi; + + @Test + public void download() throws Exception { + String fileKey = "6302d742-ebc8-4649-95cf-62ccf57a1add"; + Response response = downLoadApi.download(fileKey); + ResponseBody responseBody = response.body(); + // 二进制流 + InputStream is = responseBody.byteStream(); + + // 具体如何处理二进制流,由业务自行控制。这里以写入文件为例 + File tempDirectory = new File("temp"); + if (!tempDirectory.exists()) { + tempDirectory.mkdir(); + } + File file = new File(tempDirectory, UUID.randomUUID().toString()); + if (!file.exists()) { + file.createNewFile(); + } + FileOutputStream fos = new FileOutputStream(file); + byte[] b = new byte[1024]; + int length; + while ((length = is.read(b)) > 0) { + fos.write(b, 0, length); + } + is.close(); + fos.close(); + } +} + +### 动态URL 使用`@url`注解可实现动态URL。 diff --git a/README_EN.md b/README_EN.md index 57a3122..6c08766 100644 --- a/README_EN.md +++ b/README_EN.md @@ -769,6 +769,57 @@ Void upload(@Part MultipartBody.Part file); ``` +### download file + +#### http download interface + +```java +@RetrofitClient(baseUrl = "https://img.ljcdn.com/hc-picture/") +public interface DownloadApi { + + @GET("{fileKey}") + Response download(@Path("fileKey") String fileKey); +} + +``` + +#### http download usage + +@SpringBootTest(classes = RetrofitTestApplication.class) +@RunWith(SpringRunner.class) +public class DownloadTest { + + @Autowired + DownloadApi downLoadApi; + + @Test + public void download() throws Exception { + String fileKey = "6302d742-ebc8-4649-95cf-62ccf57a1add"; + Response response = downLoadApi.download(fileKey); + ResponseBody responseBody = response.body(); + // InputStream + InputStream is = responseBody.byteStream(); + + // The specific handling of binary streams is controlled by the business itself. Here is an example of writing a file. + File tempDirectory = new File("temp"); + if (!tempDirectory.exists()) { + tempDirectory.mkdir(); + } + File file = new File(tempDirectory, UUID.randomUUID().toString()); + if (!file.exists()) { + file.createNewFile(); + } + FileOutputStream fos = new FileOutputStream(file); + byte[] b = new byte[1024]; + int length; + while ((length = is.read(b)) > 0) { + fos.write(b, 0, length); + } + is.close(); + fos.close(); + } +} + ### Dynamic URL example Realize dynamic URL through `@url` annotation diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/DownloadTest.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/DownloadTest.java new file mode 100644 index 0000000..97a6b7f --- /dev/null +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/DownloadTest.java @@ -0,0 +1,54 @@ +package com.github.lianjiatech.retrofit.spring.boot.test; + +import com.github.lianjiatech.retrofit.spring.boot.test.http.DownloadApi; +import okhttp3.ResponseBody; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import retrofit2.Response; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.util.UUID; + +/** + * @author 陈添明 + */ + +@SpringBootTest(classes = RetrofitTestApplication.class) +@RunWith(SpringRunner.class) +public class DownloadTest { + + @Autowired + DownloadApi downLoadApi; + + @Test + public void download() throws Exception { + String fileKey = "6302d742-ebc8-4649-95cf-62ccf57a1add"; + Response response = downLoadApi.download(fileKey); + ResponseBody responseBody = response.body(); + // 二进制流 + InputStream is = responseBody.byteStream(); + + // 具体如何处理二进制流,由业务自行控制。这里以写入文件为例 + File tempDirectory = new File("temp"); + if (!tempDirectory.exists()) { + tempDirectory.mkdir(); + } + File file = new File(tempDirectory, UUID.randomUUID().toString()); + if (!file.exists()) { + file.createNewFile(); + } + FileOutputStream fos = new FileOutputStream(file); + byte[] b = new byte[1024]; + int length; + while ((length = is.read(b)) > 0) { + fos.write(b, 0, length); + } + is.close(); + fos.close(); + } +} diff --git a/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/DownloadApi.java b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/DownloadApi.java new file mode 100644 index 0000000..904ad8d --- /dev/null +++ b/src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/DownloadApi.java @@ -0,0 +1,19 @@ +package com.github.lianjiatech.retrofit.spring.boot.test.http; + +import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitClient; +import okhttp3.ResponseBody; +import retrofit2.Response; +import retrofit2.http.GET; +import retrofit2.http.Path; + +/** + * 文件下载API + * + * @author 陈添明 + */ +@RetrofitClient(baseUrl = "https://img.ljcdn.com/hc-picture/") +public interface DownloadApi { + + @GET("{fileKey}") + Response download(@Path("fileKey") String fileKey); +}