这篇文章将为大家详细讲解有关Java中怎么批量下载网络图片,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
先来看下Json数据格式:
为了方便操作,我封装了一个数据实体类
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; public class HelpUtils { 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; } public List<Bean> changeJsonToList(String json) { // 利用Gson将JSON数据反序列化成JAVA对象 Gson gson = new Gson(); List<Bean> beans = gson.fromJson(json, new TypeToken<List<Bean>>() { }.getType()); return beans; } 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<Bean> beans = helpUtils.changeJsonToList(mJsonInfo); //循环遍历下载图片 for (int i = 0; i < beans.size(); i++) { helpUtils.makeImage(beans.get(i), "C:/images/"); } } }
关于Java中怎么批量下载网络图片就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。