Java 如何将 HttpResponse 下载到文件中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19733612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to download an HttpResponse into a file?
提问by user1023127
My android app uses an API which sends a multipart HTTP request. I am successfully getting the response like so:
我的 android 应用程序使用发送多部分 HTTP 请求的 API。我成功地得到了这样的回应:
post.setEntity(multipartEntity.build());
HttpResponse response = client.execute(post);
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".
响应是电子书文件(通常是 epub 或 mobi)的内容。我想将其写入具有指定路径的文件,让我们说“/sdcard/test.epub”。
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!
文件可能高达 20MB,所以它需要使用某种流,但我无法理解它。谢谢!
采纳答案by Blackbelt
well it is a simple task, you need the WRITE_EXTERNAL_STORAGE
use permission.. then simply retrieve the InputStream
好吧,这是一项简单的任务,您需要WRITE_EXTERNAL_STORAGE
使用权限.. 然后只需检索InputStream
InputStream is = response.getEntity().getContent();
Create a FileOutputStream
创建一个文件输出流
FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "test.epub"))
;
FileOutputStream fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "test.epub"))
;
and read from is and write with fos
并从 is 读取并使用 fos 写入
int read = 0;
byte[] buffer = new byte[32768];
while( (read = is.read(buffer)) > 0) {
fos.write(buffer, 0, read);
}
fos.close();
is.close();
Edit, check for tyoo
编辑,检查 tyoo
回答by Bek
First is the method:
首先是方法:
public HttpResponse setHTTPConnection() throws IOException, NullPointerException, URISyntaxException {
HttpClient client = HttpClientBuilder.create().build();
HttpRequestBase requestMethod = new HttpGet();
requestMethod.setURI(new URI("***FileToDownlod***"));
BasicHttpContext localContext = new BasicHttpContext();
return client.execute(requestMethod, localContext);
}
Second is an actual code:
其次是实际代码:
File downloadedFile = new File("filePathToSave");
HttpResponse fileToDownload = setHTTPConnection();
try {
FileUtils.copyInputStreamToFile(fileToDownload.getEntity().getContent(), downloadedFile);
} finally {
fileToDownload.getEntity().getContent().close();
}
Please make sure to change "filePathToSave" to where to save the file and "FileToDownlod" to from where to download accordingly.
"filePathToSave" is where you want to save the file, if you choose to save it locally then you can simply point to the desktop but don't forget to give a name to your file like "/Users/admin/Desktop/downloaded.pdf" (in Mac).
"FileToDownlod" is in the form of URL e.g "https://www.doesntexist.com/sample.pdf"
Don't panic as the second piece will ask to declare in throws clause or catch multiple exceptions. This piece of code was for a specific purpose please customize for your own need.
请确保将“ filePathToSave”更改为保存文件的位置,并将“ FileToDownlod”更改为相应的下载位置。
“ filePathToSave”是您想要保存文件的位置,如果您选择将其保存在本地,那么您只需指向桌面,但不要忘记为您的文件命名,例如“/Users/admin/Desktop/downloaded.txt”。 pdf”(在 Mac 中)。
“ FileToDownlod”是URL的形式,例如“ https://www.doesntexist.com/sample.pdf”
不要惊慌,因为第二部分会要求在 throws 子句中声明或捕获多个异常。这段代码用于特定目的,请根据您自己的需要进行定制。