如何将HttpResponse下载到文件中?

如何将HttpResponse下载到文件中?,第1张

概述我的 Android应用程序使用API​​发送多部分HTTP请求.我成功地得到了这样的响应: post.setEntity(multipartEntity.build());HttpResponse response = client.execute(post); 响应是电子书文件(通常是epub或mobi)的内容.我想将其写入具有指定路径的文件,让我们说“/sdcard/test.epub”. 我的 Android应用程序使用API​​发送多部分http请求.我成功地得到了这样的响应:
post.setEntity(multipartentity.build());httpResponse response = clIEnt.execute(post);

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

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

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

创建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,read);}fos.close();is.close();

编辑,检查tyoo

总结

以上是内存溢出为你收集整理的如何将HttpResponse下载到文件中?全部内容,希望文章能够帮你解决如何将HttpResponse下载到文件中?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1133657.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存