如何将HttpResponse下载到文件中?

use*_*127 6 java apache android

我的Android应用程序使用API​​发送多部分HTTP请求.我成功地得到了这样的响应:

post.setEntity(multipartEntity.build());
HttpResponse response = client.execute(post);
Run Code Online (Sandbox Code Playgroud)

响应是电子书文件(通常是epub或mobi)的内容.我想将其写入具有指定路径的文件,让我们说"/sdcard/test.epub".

文件可能高达20MB,所以它需要使用某种流,但我可以无法绕过它.谢谢!

Bla*_*elt 14

这是一个简单的任务,你需要WRITE_EXTERNAL_STORAGE使用权限..然后只需检索InputStream

InputStream is = response.getEntity().getContent();
Run Code Online (Sandbox Code Playgroud)

创建FileOutputStream

FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "test.epub"));

读取是用fos写的

int read = 0;
byte[] buffer = new byte[32768];
while( (read = is.read(buffer)) > 0) {
  fos.write(buffer, 0, read);
}

fos.close();
is.close();
Run Code Online (Sandbox Code Playgroud)

编辑,检查tyoo