标签:
就是写的个小程序,用于记录一下,方便后续查看,首先感谢下面这个博客,从这篇文章衍生的吧,大家可以学习下:
http://www.cnblogs.com/lichenwei/p/4610298.html
读了这边文章,觉得很有意思,就模仿着写了个,从百度图片所有并下载到本地,主要思路是一样的。
贴下代码:
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 { /* * 唯一实例 */ private static IOUtils instance; /** * 单例模式 * @return IOUtils实例 * @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; } } /** * 将inputStream转换成String * @param is inputStream * @return 转换后的Sring * * @return String [返回类型说明] * @author count 2015-7-8 */ public String conventInputStream2String(InputStream is) { BufferedReader br = new BufferedReader(new InputStreamReader(is)); //线程安全 Stringbuilder 线程非安全 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(); } /** * 将inputStream写成文件保存在本地 * @param is inputStream流 * @param path 文件路径 * @param fileName 文件名 * * @return void [返回类型说明] * @author count 2015-7-8 */ public void conventInpputStream2File(InputStream is, 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) { /* * 搜索关键字 */ String wordKey = "girl"; /* * inputSream */ InputStream is = null; try { String urlencodeKewy = URLEncoder.encode(wordKey, "GBK"); //百度图片url,返回图片的json数组 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(); //获取url对应的inputStream is = connect.getInputStream(); //转换后的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("一共查到" + (imageDataJsonArray.size() - 1) + "张图片。"); url = new URL(imageJosn.getString("objURL")); try { connect = (HttpURLConnection) url.openConnection(); is = connect.getInputStream(); System.out.println("正在下载第" + i + "张,该图片的json流是" + imageJosn.getString("objURL")); System.out.println("图片名称:" + imageJosn.getString("di")); IOUtils.getIOUtilsInstance().conventInpputStream2File(is, "c:/images/" + wordKey + "/", imageJosn.getString("di") + "." + imageJosn.getString("type")); } catch (IOException ioe) { System.out.println("正在下载第" + i + "张,该图片的json流是" + imageJosn.getString("objURL")); System.out.println("图片名称:" + imageJosn.getString("di")); IOUtils.getIOUtilsInstance().conventInpputStream2File(is, "c:/images/" + wordKey + "/", imageJosn.getString("di") + "." + imageJosn.getString("type")); } } } catch (Exception e) { System.out.println(e); } } }
可能还有不完善的地方,希望大家指出,继续完善。
标签:
原文地址:http://www.cnblogs.com/countguo/p/4631721.html