如何将HttpResponse下载到文件中?

前端之家收集整理的这篇文章主要介绍了如何将HttpResponse下载到文件中?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的 Android应用程序使用API​​发送多部分HTTP请求.我成功地得到了这样的响应:
  1. post.setEntity(multipartEntity.build());
  2. HttpResponse response = client.execute(post);

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

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

解决方法@H_404_10@
这是一个简单的任务,您需要WRITE_EXTERNAL_STORAGE使用权限..然后只需检索InputStream
  1. InputStream is = response.getEntity().getContent();

创建FileOutputStream

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

读取是用fos写的

  1. int read = 0;
  2. byte[] buffer = new byte[32768];
  3. while( (read = is.read(buffer)) > 0) {
  4. fos.write(buffer,read);
  5. }
  6.  
  7. fos.close();
  8. is.close();

编辑,检查tyoo

猜你在找的Android相关文章