is to write a small program, used for recording, convenient follow-up review, first thanks to the following this blog, from this article derived from it, we can learn:
Http://www.cnblogs.com/lichenwei/p/4610298.html
Read this article, feel very interesting, imitate wrote a, from Baidu pictures and download to the local, the main idea is the same.
Paste the code:
Package Com.guo.common;import Java.io.bufferedreader;import Java.io.bufferedwriter;import java.io.File;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.io.inputstreamreader;import Java.io.outputstream;import Java.io.outputstreamwriter;public class ioutils{/* * Unique instance */private static ioutils instance; /** * Singleton mode * @return Ioutils instance * @author Count 2015-7-8 */public static ioutils getioutilsinstance () {if (null! = instance) {return instance; } synchronized (Ioutils.class) {if (null = = Instance) {instance = new Ioutils (); } return instance; }}/** * Convert InputStream to String * @param is InputStream * @return converted sring * * @return STR ing [return type description] * @author count 2015-7-8 */Public String ConventInputstream2string (InputStream is) {bufferedreader br = new BufferedReader (new InputStreamReader (IS)); Thread-Safe Stringbuilder thread non-secure stringbuffer Stringbuilder sb = new Stringbuilder (); try {String line = ""; while (null! = (line = Br.readline ())) {sb.append (line); }} catch (Exception e) {System.out.println (e); } finally {try {is.close (); Br.close (); } catch (Exception E2) {SYSTEM.OUT.PRINTLN (E2); }} return sb.tostring (); }/** * Save InputStream as file in local * @param is inputstream stream * @param path filename * @param filename * * @return void [return type description] * @author count 2015-7-8 */public void Conventinpputstream2file (InputStream i s, string path, string FileName) {File File = new file (path); if (!file.exists () | |!file.isdirectory ()) {file.mkdirs (); } file = new file (path + fileName); OutputStream OS = null; try {os = new FileOutputStream (file); int length; byte[] data = new byte[2048]; while (length = is.read (data))! =-1) {os.write (data, 0, length); }} catch (FileNotFoundException e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); } finally {try {is.close (); Os.close (); } catch (Exception e2) {}}}}
Package Com.guo.main;import Java.io.ioexception;import Java.io.inputstream;import java.net.HttpURLConnection; Import Java.net.url;import Java.net.urlencoder;import Com.alibaba.fastjson.jsonarray;import Com.alibaba.fastjson.jsonobject;import Com.guo.common.ioutils;public class downpicfrombaidu{public static void main ( String[] args) {/* * search keywords */String wordkey = "Girl"; /* * Inputsream */InputStream is = null; try {String urlencodekewy = Urlencoder.encode (Wordkey, "GBK"); Baidu image URL, return the image of the JSON array String baiduimageurl = "http://image.baidu.com/i?tn=baiduimagejson&width=&height= &word= "+ urlencodekewy +" &rn=100&pn=2 "; URL url = new URL (baiduimageurl); HttpURLConnection connect = (httpurlconnection) url.openconnection (); Gets the URL corresponding to InputStream is = connect.getInputStream (); The converted string String baiduimagestr = Ioutils.getioutilsinstance (). Conventinputstream2string ( IS); Jsonobject Baiduimagejson = Jsonobject.parseobject (BAIDUIMAGESTR); Jsonarray Imagedatajsonarray = Baiduimagejson.getjsonarray ("Data"); System.out.println (Imagedatajsonarray.tostring ()); for (int i = 0; i < imagedatajsonarray.size ()-1; i++) {Jsonobject imagejosn = (jsonobject) Imagedatajsonarray.get (i); System.out.println ("Find out" + (Imagedatajsonarray.size ()-1) + "picture. "); url = new URL (imagejosn.getstring ("Objurl")); try {connect = (httpurlconnection) url.openconnection (); is = Connect.getinputstream (); SySTEM.OUT.PRINTLN ("Downloading section" + i + "Zhang, the image of the JSON stream is" + imagejosn.getstring ("Objurl")); System.out.println ("Picture name:" + imagejosn.getstring ("Di")); Ioutils.getioutilsinstance (). Conventinpputstream2file (IS, "c:/images/" + Wordkey + "/", Imagejosn.getstring ("di") + "." + imagejosn.getstring ("type")); } catch (IOException IoE) {System.out.println ("downloading section" + i + "Zhang, the picture's JSON stream Is "+ imagejosn.getstring (" Objurl ")); System.out.println ("Picture name:" + imagejosn.getstring ("Di")); Ioutils.getioutilsinstance (). Conventinpputstream2file (IS, "c:/images/" + Wordkey + "/", Imagejosn.getstring ("di") + "." + Imagejosn.GetString ("type")); }}} catch (Exception e) {System.out.println (e); } }}
Perhaps there are imperfect places, I hope you point out, continue to improve.
Java implementation batch Download Baidu image search to the picture