Skip to content

Commit

Permalink
文件上传示例
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianming11 committed Jan 7, 2021
1 parent bd88250 commit 08b4078
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
.DS_Store
.idea/
*.iml

temp
**/pom.xml.releaseBackup
55 changes: 53 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ retrofit:

## 其他功能示例

### 上传文件示例
### 上传文件

#### 构建MultipartBody.Part

Expand All @@ -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<ResponseBody> 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<ResponseBody> 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。

Expand Down
51 changes: 51 additions & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResponseBody> 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<ResponseBody> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ResponseBody> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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<ResponseBody> download(@Path("fileKey") String fileKey);
}

0 comments on commit 08b4078

Please sign in to comment.