基于Java實現(xiàn)批量下載網(wǎng)絡圖片
昨天朋友做項目遇到一個需求,需要把上千個的微博表情圖片下載到本地磁盤,并做好規(guī)范命名,塞給我一堆Json數(shù)據(jù),讓我?guī)兔μ幚硐?,反正閑著也沒事干,就幫忙寫了。(很簡單的一個功能,隨手記錄下,剛好填補下最近博客的空白)
由于只是方便自己的工具,就不需要什么圖形界面了,就用Java去寫了,先看下效果圖~
嘿嘿,突然發(fā)現(xiàn)會寫程序是件好事,一千多張表情圖片要是手動下載再進行改名,非得忙個2天2夜不可。。
好了,言歸正傳,說下代碼實現(xiàn),分成3步:
1、獲取Json數(shù)據(jù)
2、根據(jù)Json數(shù)據(jù)所提供的圖片資源地址進行下載
3、分類,規(guī)范命名
先來看下Json數(shù)據(jù)格式:
為了方便操作,我封裝了一個數(shù)據(jù)實體類
- 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 + "]";
- }
- }
然后我寫了一個工具類封裝了一些方法
分別用來處理(網(wǎng)絡數(shù)據(jù)的獲取,Json數(shù)據(jù)的反序列化,對圖片資源的下載)
- 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 {
- /**
- * 根據(jù)所提供的url地址獲取Json數(shù)據(jù)
- *
- * @param path
- * @return
- */
- public String getHttpString(String path) {
- // 存放獲取到的數(shù)據(jù)
- String info = "";
- // 網(wǎng)絡請求所需變量
- InputStream in = null;
- InputStreamReader reader = null;
- BufferedReader bufferedReader = null;
- try {
- URL url = new URL(path);
- // 根據(jù)Url打開地址,以utf-8編碼的形式返回輸入流
- in = url.openStream();
- reader = new InputStreamReader(in, "utf-8");
- bufferedReader = new BufferedReader(reader);
- // 臨時接受數(shù)據(jù)變量
- 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數(shù)據(jù)反序列化成Java對象(List集合)
- *
- * @param json
- * @return
- */
- public List<Bean> changeJsonToList(String json) {
- // 利用Gson將JSON數(shù)據(jù)反序列化成JAVA對象
- Gson gson = new Gson();
- List<Bean> beans = gson.fromJson(json, new TypeToken<List<Bean>>() {
- }.getType());
- return beans;
- }
- /**
- * 下載圖片,并按照指定的路徑存儲
- * @param bean
- * @param filePath
- */
- public void makeImage(Bean bean, String filePath) {
- // 網(wǎng)絡請求所需變量
- try {
- //獲取輸入流
- BufferedInputStream in = new BufferedInputStream(new URL(bean.getUrl()).openStream());
- //創(chuàng)建文件流
- File file = new File(filePath + bean.getPhrase()+".gif");
- BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
- //緩沖字節(jié)數(shù)組
- 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("正在執(zhí)行下載任務:當前正在下載圖片" + bean.getPhrase() + ".gif");
- in.close();
- out.close();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
上面代碼對于Json數(shù)據(jù)的處理,我用到了谷歌給我們提供的Gson工具類
對于Gson類不懂使用的朋友可以看下我之前寫過的一篇文章:
《Gson簡要使用筆記》:http://www.cnblogs.com/lichenwei/p/3987429.html
- 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 = "這里涉及到Oauth2.0的一些個人隱私數(shù)據(jù)就不給出了";
- private static String mJsonInfo;
- public static void main(String[] args) {
- HelpUtils helpUtils = new HelpUtils();
- // 獲取Json數(shù)據(jù)
- mJsonInfo = helpUtils.getHttpString(URL);
- // 將Json數(shù)據(jù)反序列化成java對象
- List<Bean> beans = helpUtils.changeJsonToList(mJsonInfo);
- //循環(huán)遍歷下載圖片
- for (int i = 0; i < beans.size(); i++) {
- helpUtils.makeImage(beans.get(i), "C:/images/");
- }
- }
- }
到這里就完事了,有哪里不清楚的朋友,可以在下面文章評論交流。
作者:Balla_兔子
出處:http://www.cnblogs.com/lichenwei/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。
正在看本人博客的這位童鞋,我看你氣度不凡,談吐間隱隱有王者之氣,日后必有一番作為!旁邊有“推薦”二字,你就順手把它點了吧,相得準,我分文不收;相不準,你也好回來找我!