如何下载的Htt presponse到一个文件? [英] How to download an HttpResponse into a file?
问题描述
我的Android应用程序使用它发送一个多HTTP请求的API。我成功地得到像这样的回应:
My android app uses an API which sends a multipart HTTP request. I am successfully getting the response like so:
post.setEntity(multipartEntity.build());
HttpResponse response = client.execute(post);
的响应是一个电子书文件(通常是EPUB或牧高笛)的内容。我想写这与指定的路径下的文件,可以说/sdcard/test.epub。
The response is the contents of an ebook file (usually an epub or mobi). I want to write this to a file with a specified path, lets says "/sdcard/test.epub".
文件可达到20MB,所以需要使用某种流,但我就是不能换我的头周围。谢谢!
File could be up to 20MB, so it'll need to use some sort of stream, but I can just can't wrap my head around it. Thanks!
推荐答案
以及它是一个简单的任务,你需要的 WRITE_EXTERNAL_STORAGE
使用许可..然后只检索的InputStream
well it is a simple task, you need the WRITE_EXTERNAL_STORAGE
use permission.. then simply retrieve the InputStream
InputStream is = response.getEntity().getContent();
创建一个FileOutputStream
Create a FileOutputStream
的FileOutputStream FOS =新的FileOutputStream(新文件(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();
编辑,检查tyoo
Edit, check for tyoo
这篇关于如何下载的Htt presponse到一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!