java 版 获取css中的图片 httpClient4.1.3 下载

  要下载源代码java 版 获取css中的图片 httpClient4.1.3 下载

先看运行效果

 

这是选择文件 ,可以选择css和js

 

 

 

选择文件存放的路径

 

分析css中图片的url

 

填写网站的主路径很重要,下载图片就根据这个url和css中的url结合起来

这样就可以下载成功了。

部分  关键代码

public static String downLoad(HttpClient httpclient,String url,String savePath,String imgName) throws Exception, IOException{
  
  HttpGet httpGet = new HttpGet(url);
  HttpResponse response = httpclient.execute(httpGet);

  
  if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
   HttpEntity entity = response.getEntity();

   if (entity != null) {
    System.out.println(entity.getContentType());
    // 可以判断是否是文件数据流
    System.out.println(entity.isStreaming());
    // 设置本地保存的文件
    //File storeFile = new File("F:/"+imgName);
    File storeFile = new File(savePath+"/"+imgName);
    FileOutputStream output = new FileOutputStream(storeFile);
    // 得到网络资源并写入文件
    InputStream input = entity.getContent();
    byte b[] = new byte[1024];
    int j = 0;
    while ((j = input.read(b)) != -1) {
     output.write(b, 0, j);
    }
    output.flush();
    output.close();
   }
   if (entity != null) {
    entity.consumeContent();
   }
   
   return "成功下载     "+url;
}
  else {
   
   httpGet.abort();
   return "失败下载     "+url;
  }
  
  
 }

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值