Batch download of network images and Java Network Images Based on java

Source: Internet
Author: User

Batch download of network images and Java Network Images Based on java

Yesterday, my friend encountered a requirement for a project. He needed to download thousands of Weibo emoticon images to a local disk and name them properly. Then, he gave me a bunch of Json data and asked me to handle it, no matter whether you are idle or not, you can help write it. (This is a simple function. It just fills in the blank of the recent blog)

Because it is just a convenient tool, you don't need any graphical interface. You can use Java to write it first ~

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 @ Override88 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 92}

Some methods are encapsulated for processing (network data acquisition, Json data deserialization, and image resource download)

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 1 5 import com. google. gson. gson; 16 import com. google. gson. reflect. typeToken; 17 import com. lcw. downloadutil. domain. bean; 18 19/** 20 * tool class set 21*22 * @ author Rabbit_Lee 23*24 */25 public class HelpUtils {26/** 27 * Get Json according to the provided url data 28*29 * @ param path 30 * @ return 31 */32 public String getHttpString (String path) {33 // store the obtained data 34 String info = ""; 35 // The variable 36 InputStream in = n for the network request Ull; 37 InputStreamReader reader = null; 38 BufferedReader bufferedReader = null; 39 try {40 URL url = new URL (path); 41 // open the address based on the Url, returns the input stream 42 in = url in UTF-8 encoding format. openStream (); 43 reader = new InputStreamReader (in, "UTF-8"); 44 bufferedReader = new BufferedReader (reader); 45 // tentatively accept the data variable 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 * deserialize the provided Json data into a Java object (List set) 69*70 * @ param json 71 * @ return 72 */73 public List <Bean> changeJsonToList (String json) {74 // deserialize JSON data into a JAVA object using Gson 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 * download image, and store 83 * @ param bean 84 * @ param filePath 85 */86 public void makeImage (Bean bean, String filePath) according to the specified path) {87 // variable 88 try {89 for network requests // Obtain the input stream 90 BufferedInputStream in = new BufferedInputStream (new URL (bean. getUrl ()). openStream (); 91 // create a File stream 92 file File = new File (filePath + bean. getPhrase () + ". gif "); 93 BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (file); 94 // buffer byte array 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 ("executing download task: currently downloading image" + 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 111}

The above code processes Json data. I used the Gson tool class that Google provided to us.

For Gson class do not understand the use of friends can read I have written before an article: Gson brief use notes: http://www.cnblogs.com/lichenwei/p/3987429.html

Next, the main class is called:

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 = "some private data related to Oauth2.0 is not provided here"; 11 private static String mJsonInfo; 12 13 public static void main (String [] args) {14 HelpUtils helpUtils = new HelpUtils (); 15 // get Json data 16 mJsonInfo = helpUtils. getHttpString (URL); 17 // deserializes Json data into java object 18 List <Bean> beans = helpUtils. changeJsonToList (mJsonInfo); 19 // loop traversal download image 20 for (int I = 0; I <beans. size (); I ++) {21 helpUtils. makeImage (beans. get (I), "C:/images/"); 22} 23 24} 25 26}

 

This is the end of the process. If you have any questions, you can comment on the article.

Author: Balla _ rabbit
Source: http://www.cnblogs.com/lichenwei/
The copyright of this article is shared by the author and the blog. You are welcome to repost this article, but you must keep this statement without the author's consent and provide the original article link clearly on the article page.
I am reading this kid's shoes from my blog. I think you are so angry that you have a secret of the king in your conversation. You will do something in the future! With the word "recommendation" next to it, you can just click it. it's accurate. I don't accept anything. If you're not sure, you can come back to me!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.