java 是否可以使用 HttpClient 下载 PDF 之类的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10770483/
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
Is it possible to download files like PDF with HttpClient?
提问by code511788465541441
I found some examples here on how to download a file but most of them seem to be using HttpURLConnection. is it possible to download files with HttpClient?
我在这里找到了一些关于如何下载文件的示例,但其中大多数似乎都在使用 HttpURLConnection。是否可以使用 HttpClient 下载文件?
回答by Matt
Using httpclient is pretty easy. Here's a link to it's tutorial.
使用 httpclient 非常简单。这是它的教程的链接。
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e43
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e43
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(urltofetch);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
long len = entity.getContentLength();
InputStream inputStream = entity.getContent();
// write the file to whether you want it.
}
回答by Francis Upton IV
Anything you can do with HttpURLConnection
you can do, usually better, with HttpClient
look through their examples about file transfer and you will see how.
您可以做的任何事情HttpURLConnection
通常都可以做得更好,通过HttpClient
查看他们关于文件传输的示例,您将了解如何做。