成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

基于Java實(shí)現(xiàn)批量下載網(wǎng)絡(luò)圖片

開發(fā) 后端 前端
昨天朋友做項(xiàng)目遇到一個(gè)需求,需要把上千個(gè)的微博表情圖片下載到本地磁盤,并做好規(guī)范命名,塞給我一堆Json數(shù)據(jù),讓我?guī)兔μ幚硐?,反正閑著也沒事干,就幫忙寫了。(很簡單的一個(gè)功能,隨手記錄下,剛好填補(bǔ)下最近博客的空白)

昨天朋友做項(xiàng)目遇到一個(gè)需求,需要把上千個(gè)的微博表情圖片下載到本地磁盤,并做好規(guī)范命名,塞給我一堆Json數(shù)據(jù),讓我?guī)兔μ幚硐?,反正閑著也沒事干,就幫忙寫了。(很簡單的一個(gè)功能,隨手記錄下,剛好填補(bǔ)下最近博客的空白)

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



嘿嘿,突然發(fā)現(xiàn)會(huì)寫程序是件好事,一千多張表情圖片要是手動(dòng)下載再進(jìn)行改名,非得忙個(gè)2天2夜不可。。

好了,言歸正傳,說下代碼實(shí)現(xiàn),分成3步:

1、獲取Json數(shù)據(jù)

2、根據(jù)Json數(shù)據(jù)所提供的圖片資源地址進(jìn)行下載

3、分類,規(guī)范命名 

先來看下Json數(shù)據(jù)格式:

為了方便操作,我封裝了一個(gè)數(shù)據(jù)實(shí)體類

  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.  

然后我寫了一個(gè)工具類封裝了一些方法

分別用來處理(網(wǎng)絡(luò)數(shù)據(jù)的獲取,Json數(shù)據(jù)的反序列化,對圖片資源的下載)

  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.      * 根據(jù)所提供的url地址獲取Json數(shù)據(jù) 
  28.      *  
  29.      * @param path 
  30.      * @return 
  31.      */ 
  32.     public String getHttpString(String path) { 
  33.         // 存放獲取到的數(shù)據(jù) 
  34.         String info = ""
  35.         // 網(wǎng)絡(luò)請求所需變量 
  36.         InputStream in = null
  37.         InputStreamReader reader = null
  38.         BufferedReader bufferedReader = null
  39.         try { 
  40.             URL url = new URL(path); 
  41.             // 根據(jù)Url打開地址,以utf-8編碼的形式返回輸入流 
  42.             in = url.openStream(); 
  43.             reader = new InputStreamReader(in, "utf-8"); 
  44.             bufferedReader = new BufferedReader(reader); 
  45.             // 臨時(shí)接受數(shù)據(jù)變量 
  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數(shù)據(jù)反序列化成Java對象(List集合) 
  69.      *  
  70.      * @param json 
  71.      * @return 
  72.      */ 
  73.     public List<Bean> changeJsonToList(String json) { 
  74.         // 利用Gson將JSON數(shù)據(jù)反序列化成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.      * 下載圖片,并按照指定的路徑存儲(chǔ) 
  83.      * @param bean 
  84.      * @param filePath 
  85.      */ 
  86.     public void makeImage(Bean bean, String filePath) { 
  87.         // 網(wǎng)絡(luò)請求所需變量 
  88.         try { 
  89.             //獲取輸入流 
  90.             BufferedInputStream in = new BufferedInputStream(new URL(bean.getUrl()).openStream()); 
  91.             //創(chuàng)建文件流 
  92.             File file = new File(filePath + bean.getPhrase()+".gif"); 
  93.             BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); 
  94.             //緩沖字節(jié)數(shù)組 
  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("正在執(zhí)行下載任務(wù):當(dāng)前正在下載圖片" + 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.  

上面代碼對于Json數(shù)據(jù)的處理,我用到了谷歌給我們提供的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的一些個(gè)人隱私數(shù)據(jù)就不給出了"
  11.     private static String mJsonInfo; 
  12.  
  13.     public static void main(String[] args) { 
  14.         HelpUtils helpUtils = new HelpUtils(); 
  15.         // 獲取Json數(shù)據(jù) 
  16.         mJsonInfo = helpUtils.getHttpString(URL); 
  17.         // 將Json數(shù)據(jù)反序列化成java對象 
  18.         List<Bean> beans = helpUtils.changeJsonToList(mJsonInfo); 
  19.         //循環(huán)遍歷下載圖片 
  20.         for (int i = 0; i < beans.size(); i++) { 
  21.             helpUtils.makeImage(beans.get(i), "C:/images/"); 
  22.         } 
  23.  
  24.     } 
  25.  

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

作者:Balla_兔子
出處:http://www.cnblogs.com/lichenwei/
本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。
正在看本人博客的這位童鞋,我看你氣度不凡,談吐間隱隱有王者之氣,日后必有一番作為!旁邊有“推薦”二字,你就順手把它點(diǎn)了吧,相得準(zhǔn),我分文不收;相不準(zhǔn),你也好回來找我!

責(zé)任編輯:王雪燕 來源: 博客園
相關(guān)推薦

2010-01-27 17:53:18

Android顯示網(wǎng)絡(luò)

2015-04-17 10:31:11

PHP下載美女圖片實(shí)現(xiàn)代碼

2015-06-11 10:12:26

Android圖片加載緩存

2013-05-14 15:08:02

2009-11-24 14:45:08

PHP批量上傳圖片

2022-08-05 19:27:22

通用API鴻蒙

2012-12-25 13:54:28

AndroidTextview

2011-07-20 17:29:12

iPhone 網(wǎng)絡(luò)

2021-02-22 15:30:07

Python 開發(fā)編程語言

2021-12-27 10:40:13

Kubernetes網(wǎng)絡(luò)圖解Linux

2010-02-26 15:09:59

Linux NFS

2018-03-15 13:10:33

思科網(wǎng)絡(luò)技術(shù)智慧系統(tǒng)

2021-03-12 10:40:46

CycleGAN網(wǎng)絡(luò)圖像深度學(xué)習(xí)

2017-04-19 12:28:27

2023-12-06 09:33:54

Reactor網(wǎng)絡(luò)

2020-12-19 11:05:57

循環(huán)神經(jīng)網(wǎng)絡(luò)PyTorch神經(jīng)網(wǎng)絡(luò)

2011-03-09 10:07:56

網(wǎng)絡(luò)爬蟲Java

2014-05-04 11:02:58

jenkins服務(wù)器批量

2010-11-29 13:17:00

Sybase批量操作

2011-10-31 09:35:50

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 欧美黑人一区 | 日本精品视频一区二区 | www亚洲精品| 黄色小视频入口 | 大陆一级毛片免费视频观看 | 精品久久久久久亚洲综合网 | 天天干免费视频 | 国产精品一区久久久久 | 网色| 欧美福利专区 | 亚洲一区二区免费视频 | 97色在线观看免费视频 | 在线免费观看色 | 欧美日韩一区二区三区不卡视频 | 免费大黄视频 | 最新中文字幕一区 | 欧美精品福利 | 久久久久免费 | 中文字幕在线一区二区三区 | 精品一二区 | 久久久久无码国产精品一区 | av在线天堂网 | 成人午夜视频在线观看 | 国产在线aa| 国产精品亚洲一区二区三区在线观看 | 特级生活片 | 古典武侠第一页久久777 | 日韩精品中文字幕一区二区三区 | 欧美综合在线观看 | 在线观看视频h | 欧美亚洲综合久久 | 91精品国产91久久久久久不卞 | 免费观看www7722午夜电影 | 精品视频一区二区三区在线观看 | 91中文字幕在线观看 | 久久精品综合网 | 国产乱码精品一区二区三区五月婷 | 特黄色毛片 | 亚州国产| av免费观看网站 | 日日噜噜噜夜夜爽爽狠狠视频, |