码迷,mamicode.com
首页 > Web开发 > 详细

httpclient htmlunit下载单个文件

时间:2018-08-12 11:58:43      阅读:525      评论:0      收藏:0      [点我收藏+]

标签:public   row   size   class   nload   client   exe   col   div   

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下载单个文件

标签:public   row   size   class   nload   client   exe   col   div   

原文地址:https://www.cnblogs.com/tele-share/p/9462078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!