Java中怎么批量下载网络图片-古蔺大橙子建站
RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
Java中怎么批量下载网络图片

这篇文章将为大家详细讲解有关Java中怎么批量下载网络图片,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:主机域名、虚拟空间、营销软件、网站建设、开平网站维护、网站推广。

先来看下Json数据格式:

Java中怎么批量下载网络图片

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

package com.lcw.downloadutil.domain;  public class Bean {      private String phrase;     private String type;     private String url;     private Boolean hot;     private Boolean common;     private String category;     private String icon;     private String value;     private String picid;      public String getPhrase() {         return phrase;     }      public void setPhrase(String phrase) {         this.phrase = phrase;     }      public String getType() {         return type;     }      public void setType(String type) {         this.type = type;     }      public String getUrl() {         return url;     }      public void setUrl(String url) {         this.url = url;     }      public Boolean getHot() {         return hot;     }      public void setHot(Boolean hot) {         this.hot = hot;     }      public Boolean getCommon() {         return common;     }      public void setCommon(Boolean common) {         this.common = common;     }      public String getCategory() {         return category;     }      public void setCategory(String category) {         this.category = category;     }      public String getIcon() {         return icon;     }      public void setIcon(String icon) {         this.icon = icon;     }      public String getValue() {         return value;     }      public void setValue(String value) {         this.value = value;     }      public String getPicid() {         return picid;     }      public void setPicid(String picid) {         this.picid = picid;     }      @Override     public String toString() {         return "Bean [phrase=" + phrase + ", type=" + type + ", url=" + url + ", hot=" + hot + ", common=" + common + ", category=" + category + ", icon=" + icon + ", value=" + value + ", picid=" + picid + "]";     }  }

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

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

package com.lcw.downloadutil.utils;  import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.util.List;  import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.lcw.downloadutil.domain.Bean;  /**  * 工具类集合  *   * @author Rabbit_Lee  *   */ public class HelpUtils {     /**      * 根据所提供的url地址获取Json数据      *       * @param path      * @return      */     public String getHttpString(String path) {         // 存放获取到的数据         String info = "";         // 网络请求所需变量         InputStream in = null;         InputStreamReader reader = null;         BufferedReader bufferedReader = null;         try {             URL url = new URL(path);             // 根据Url打开地址,以utf-8编码的形式返回输入流             in = url.openStream();             reader = new InputStreamReader(in, "utf-8");             bufferedReader = new BufferedReader(reader);             // 临时接受数据变量             String temp = null;             while ((temp = bufferedReader.readLine()) != null) {                 info += temp;             }             return info;         } catch (MalformedURLException e) {             e.printStackTrace();         } catch (IOException e) {             e.printStackTrace();         } finally {             try {                 in.close();                 reader.close();                 bufferedReader.close();             } catch (IOException e) {                 e.printStackTrace();             }         }         return null;     }      /**      * 将所提供的Json数据反序列化成Java对象(List集合)      *       * @param json      * @return      */     public List changeJsonToList(String json) {         // 利用Gson将JSON数据反序列化成JAVA对象         Gson gson = new Gson();         List beans = gson.fromJson(json, new TypeToken>() {         }.getType());         return beans;     }      /**      * 下载图片,并按照指定的路径存储      * @param bean      * @param filePath      */     public void makeImage(Bean bean, String filePath) {         // 网络请求所需变量         try {             //获取输入流             BufferedInputStream in = new BufferedInputStream(new URL(bean.getUrl()).openStream());             //创建文件流             File file = new File(filePath + bean.getPhrase()+".gif");             BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));             //缓冲字节数组             byte[] data = new byte[2048];             int length = in.read(data);             while (length != -1) {                 out.write(data, 0, data.length);                 length = in.read(data);             }             System.out.println("正在执行下载任务:当前正在下载图片" + bean.getPhrase() + ".gif");             in.close();             out.close();         } catch (MalformedURLException e) {             e.printStackTrace();         } catch (IOException e) {             e.printStackTrace();         }     }  }

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

package com.lcw.downloadutil.main;  import java.util.List;  import com.lcw.downloadutil.domain.Bean; import com.lcw.downloadutil.utils.HelpUtils;  public class TaskMain {      private static final String URL = "这里涉及到Oauth3.0的一些个人隐私数据就不给出了";     private static String mJsonInfo;      public static void main(String[] args) {         HelpUtils helpUtils = new HelpUtils();         // 获取Json数据         mJsonInfo = helpUtils.getHttpString(URL);         // 将Json数据反序列化成java对象         List beans = helpUtils.changeJsonToList(mJsonInfo);         //循环遍历下载图片         for (int i = 0; i < beans.size(); i++) {             helpUtils.makeImage(beans.get(i), "C:/images/");         }      }  }

关于Java中怎么批量下载网络图片就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


文章标题:Java中怎么批量下载网络图片
文章链接:http://scgulin.cn/article/ipoeis.html