-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd88250
commit 08b4078
Showing
5 changed files
with
178 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,5 @@ | |
.DS_Store | ||
.idea/ | ||
*.iml | ||
|
||
temp | ||
**/pom.xml.releaseBackup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/DownloadTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/test/java/com/github/lianjiatech/retrofit/spring/boot/test/http/DownloadApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |