httpclient htmlunit下载单个文件

Posted code never lies

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpclient htmlunit下载单个文件相关的知识,希望对你有一定的参考价值。

httpclient

 1 public class Download {
 2     public static void main(String[] args) throws ClientProtocolException, IOException {
 3         String url = "http://1251214202.vod2.myqcloud.com/8f3139d3vodgzp1251214202/c9bff9c47447398155998582539/5nFpW8vzdYAA.mp3";
 4         CloseableHttpClient client = HttpClients.createDefault();
 5         HttpGet get = new HttpGet(url);
 6         CloseableHttpResponse response = client.execute(get);
 7         if(response.getStatusLine().getStatusCode() == 200) {
 8             HttpEntity entity = response.getEntity();
 9         //    System.out.println(entity.getContentLength());
10             IOUtils.write(IOUtils.readFully(entity.getContent(),(int)entity.getContentLength()),new FileOutputStream("f:/枯叶双子.mp3"));
11         }
12         response.close();
13         get.abort();
14         client.close();
15     }
16 }

 

htmlunit

 1 public class Download {
 2     public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
 3         WebClient client = new WebClient(BrowserVersion.CHROME);
 4         String url = "http://1251214202.vod2.myqcloud.com/8f3139d3vodgzp1251214202/c9bff9c47447398155998582539/5nFpW8vzdYAA.mp3";
 5         Page  page = client.getPage(url);
 6         InputStream contentAsStream = page.getWebResponse().getContentAsStream();
 7         IOUtils.write(IOUtils.readFully(contentAsStream,(int)page.getWebResponse().getContentLength()),new FileOutputStream("f:/枯叶双子111.mp3"));
 8         client.close();
 9     }
10 }

 

以上是关于httpclient htmlunit下载单个文件的主要内容,如果未能解决你的问题,请参考以下文章

Java 实现 HttpClients+jsoup,Jsoup,htmlunit,Headless Chrome 爬虫抓取数据

Java网页抓取技术HtmlUnit

Apache HttpClient

浅析HttpClient数据推送

用htmlunit怎么去获取一个有JS加载的网页信息

网络爬虫