您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Java中怎么批量下載網(wǎng)絡(luò)圖片,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
先來看下Json數(shù)據(jù)格式:
為了方便操作,我封裝了一個(gè)數(shù)據(jù)實(shí)體類
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 + "]"; } }
然后我寫了一個(gè)工具類封裝了一些方法
分別用來處理(網(wǎng)絡(luò)數(shù)據(jù)的獲取,Json數(shù)據(jù)的反序列化,對(duì)圖片資源的下載)
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)絡(luò)請(qǐng)求所需變量 InputStream in = null; InputStreamReader reader = null; BufferedReader bufferedReader = null; try { URL url = new URL(path); // 根據(jù)Url打開地址,以u(píng)tf-8編碼的形式返回輸入流 in = url.openStream(); reader = new InputStreamReader(in, "utf-8"); bufferedReader = new BufferedReader(reader); // 臨時(shí)接受數(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對(duì)象(List集合) * * @param json * @return */ public List<Bean> changeJsonToList(String json) { // 利用Gson將JSON數(shù)據(jù)反序列化成JAVA對(duì)象 Gson gson = new Gson(); List<Bean> beans = gson.fromJson(json, new TypeToken<List<Bean>>() { }.getType()); return beans; } /** * 下載圖片,并按照指定的路徑存儲(chǔ) * @param bean * @param filePath */ public void makeImage(Bean bean, String filePath) { // 網(wǎng)絡(luò)請(qǐ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í)行下載任務(wù):當(dāng)前正在下載圖片" + bean.getPhrase() + ".gif"); in.close(); out.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
上面代碼對(duì)于Json數(shù)據(jù)的處理,我用到了谷歌給我們提供的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的一些個(gè)人隱私數(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對(duì)象 List<Bean> beans = helpUtils.changeJsonToList(mJsonInfo); //循環(huán)遍歷下載圖片 for (int i = 0; i < beans.size(); i++) { helpUtils.makeImage(beans.get(i), "C:/images/"); } } }
關(guān)于Java中怎么批量下載網(wǎng)絡(luò)圖片就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。