zoukankan      html  css  js  c++  java
  • 基于Java实现批量下载网络图片

    昨天朋友做项目遇到一个需求,需要把上千个的微博表情图片下载到本地磁盘,并做好规范命名,塞给我一堆Json数据,让我帮忙处理下,反正闲着也没事干,就帮忙写了。(很简单的一个功能,随手记录下,刚好填补下最近博客的空白)

    由于只是方便自己的工具,就不需要什么图形界面了,就用Java去写了,先看下效果图~

     

    嘿嘿,突然发现会写程序是件好事,一千多张表情图片要是手动下载再进行改名,非得忙个2天2夜不可。。

    好了,言归正传,说下代码实现,分成3步:

    1、获取Json数据

    2、根据Json数据所提供的图片资源地址进行下载

    3、分类,规范命名

    先来看下Json数据格式:

    为了方便操作,我封装了一个数据实体类

     1 package com.lcw.downloadutil.domain;
     2 
     3 public class Bean {
     4 
     5     private String phrase;
     6     private String type;
     7     private String url;
     8     private Boolean hot;
     9     private Boolean common;
    10     private String category;
    11     private String icon;
    12     private String value;
    13     private String picid;
    14 
    15     public String getPhrase() {
    16         return phrase;
    17     }
    18 
    19     public void setPhrase(String phrase) {
    20         this.phrase = phrase;
    21     }
    22 
    23     public String getType() {
    24         return type;
    25     }
    26 
    27     public void setType(String type) {
    28         this.type = type;
    29     }
    30 
    31     public String getUrl() {
    32         return url;
    33     }
    34 
    35     public void setUrl(String url) {
    36         this.url = url;
    37     }
    38 
    39     public Boolean getHot() {
    40         return hot;
    41     }
    42 
    43     public void setHot(Boolean hot) {
    44         this.hot = hot;
    45     }
    46 
    47     public Boolean getCommon() {
    48         return common;
    49     }
    50 
    51     public void setCommon(Boolean common) {
    52         this.common = common;
    53     }
    54 
    55     public String getCategory() {
    56         return category;
    57     }
    58 
    59     public void setCategory(String category) {
    60         this.category = category;
    61     }
    62 
    63     public String getIcon() {
    64         return icon;
    65     }
    66 
    67     public void setIcon(String icon) {
    68         this.icon = icon;
    69     }
    70 
    71     public String getValue() {
    72         return value;
    73     }
    74 
    75     public void setValue(String value) {
    76         this.value = value;
    77     }
    78 
    79     public String getPicid() {
    80         return picid;
    81     }
    82 
    83     public void setPicid(String picid) {
    84         this.picid = picid;
    85     }
    86 
    87     @Override
    88     public String toString() {
    89         return "Bean [phrase=" + phrase + ", type=" + type + ", url=" + url + ", hot=" + hot + ", common=" + common + ", category=" + category + ", icon=" + icon + ", value=" + value + ", picid=" + picid + "]";
    90     }
    91 
    92 }

    然后我写了一个工具类封装了一些方法

    分别用来处理(网络数据的获取,Json数据的反序列化,对图片资源的下载)

      1 package com.lcw.downloadutil.utils;
      2 
      3 import java.io.BufferedInputStream;
      4 import java.io.BufferedOutputStream;
      5 import java.io.BufferedReader;
      6 import java.io.File;
      7 import java.io.FileOutputStream;
      8 import java.io.IOException;
      9 import java.io.InputStream;
     10 import java.io.InputStreamReader;
     11 import java.net.MalformedURLException;
     12 import java.net.URL;
     13 import java.util.List;
     14 
     15 import com.google.gson.Gson;
     16 import com.google.gson.reflect.TypeToken;
     17 import com.lcw.downloadutil.domain.Bean;
     18 
     19 /**
     20  * 工具类集合
     21  * 
     22  * @author Rabbit_Lee
     23  * 
     24  */
     25 public class HelpUtils {
     26     /**
     27      * 根据所提供的url地址获取Json数据
     28      * 
     29      * @param path
     30      * @return
     31      */
     32     public String getHttpString(String path) {
     33         // 存放获取到的数据
     34         String info = "";
     35         // 网络请求所需变量
     36         InputStream in = null;
     37         InputStreamReader reader = null;
     38         BufferedReader bufferedReader = null;
     39         try {
     40             URL url = new URL(path);
     41             // 根据Url打开地址,以utf-8编码的形式返回输入流
     42             in = url.openStream();
     43             reader = new InputStreamReader(in, "utf-8");
     44             bufferedReader = new BufferedReader(reader);
     45             // 临时接受数据变量
     46             String temp = null;
     47             while ((temp = bufferedReader.readLine()) != null) {
     48                 info += temp;
     49             }
     50             return info;
     51         } catch (MalformedURLException e) {
     52             e.printStackTrace();
     53         } catch (IOException e) {
     54             e.printStackTrace();
     55         } finally {
     56             try {
     57                 in.close();
     58                 reader.close();
     59                 bufferedReader.close();
     60             } catch (IOException e) {
     61                 e.printStackTrace();
     62             }
     63         }
     64         return null;
     65     }
     66 
     67     /**
     68      * 将所提供的Json数据反序列化成Java对象(List集合)
     69      * 
     70      * @param json
     71      * @return
     72      */
     73     public List<Bean> changeJsonToList(String json) {
     74         // 利用Gson将JSON数据反序列化成JAVA对象
     75         Gson gson = new Gson();
     76         List<Bean> beans = gson.fromJson(json, new TypeToken<List<Bean>>() {
     77         }.getType());
     78         return beans;
     79     }
     80 
     81     /**
     82      * 下载图片,并按照指定的路径存储
     83      * @param bean
     84      * @param filePath
     85      */
     86     public void makeImage(Bean bean, String filePath) {
     87         // 网络请求所需变量
     88         try {
     89             //获取输入流
     90             BufferedInputStream in = new BufferedInputStream(new URL(bean.getUrl()).openStream());
     91             //创建文件流
     92             File file = new File(filePath + bean.getPhrase()+".gif");
     93             BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
     94             //缓冲字节数组
     95             byte[] data = new byte[2048];
     96             int length = in.read(data);
     97             while (length != -1) {
     98                 out.write(data, 0, data.length);
     99                 length = in.read(data);
    100             }
    101             System.out.println("正在执行下载任务:当前正在下载图片" + bean.getPhrase() + ".gif");
    102             in.close();
    103             out.close();
    104         } catch (MalformedURLException e) {
    105             e.printStackTrace();
    106         } catch (IOException e) {
    107             e.printStackTrace();
    108         }
    109     }
    110 
    111 }

    上面代码对于Json数据的处理,我用到了谷歌给我们提供的Gson工具类

    对于Gson类不懂使用的朋友可以看下我之前写过的一篇文章:

    Gson简要使用笔记》:http://www.cnblogs.com/lichenwei/p/3987429.html

    接着,就是调用主类:

     1 package com.lcw.downloadutil.main;
     2 
     3 import java.util.List;
     4 
     5 import com.lcw.downloadutil.domain.Bean;
     6 import com.lcw.downloadutil.utils.HelpUtils;
     7 
     8 public class TaskMain {
     9 
    10     private static final String URL = "这里涉及到Oauth2.0的一些个人隐私数据就不给出了";
    11     private static String mJsonInfo;
    12 
    13     public static void main(String[] args) {
    14         HelpUtils helpUtils = new HelpUtils();
    15         // 获取Json数据
    16         mJsonInfo = helpUtils.getHttpString(URL);
    17         // 将Json数据反序列化成java对象
    18         List<Bean> beans = helpUtils.changeJsonToList(mJsonInfo);
    19         //循环遍历下载图片
    20         for (int i = 0; i < beans.size(); i++) {
    21             helpUtils.makeImage(beans.get(i), "C:/images/");
    22         }
    23 
    24     }
    25 
    26 }

    到这里就完事了,有哪里不清楚的朋友,可以在下面文章评论交流。

    作者:Balla_兔子
    出处:http://www.cnblogs.com/lichenwei/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
    正在看本人博客的这位童鞋,我看你气度不凡,谈吐间隐隐有王者之气,日后必有一番作为!旁边有“推荐”二字,你就顺手把它点了吧,相得准,我分文不收;相不准,你也好回来找我!

  • 相关阅读:
    读书笔记-js
    读书笔记-设计模式
    读书笔记-并发和多线程
    读书笔记-泛型有限通配符
    读书笔记-类和类加载器
    项目: 推送水木文章到Kindle
    项目:DoubleFaceCamera
    项目:BluetoothChat
    项目:简单记事本
    项目: 连连看
  • 原文地址:https://www.cnblogs.com/lichenwei/p/4610298.html
Copyright © 2011-2022 走看看