简体   繁体   English

是否可以使用HttpClient下载PDF等文件?

[英]Is it possible to download files like PDF with HttpClient?

I found some examples here on how to download a file but most of them seem to be using HttpURLConnection. 我在这里找到了一些关于如何下载文件的例子,但是大多数文件似乎都在使用HttpURLConnection。 is it possible to download files with HttpClient? 是否可以使用HttpClient下载文件?

Using httpclient is pretty easy. 使用httpclient非常简单。 Here's a link to it's tutorial. 这是教程的链接。

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.
}

您可以使用HttpURLConnection做的任何事情,通常更好,使用HttpClient查看有关文件传输的示例,您将看到如何做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM