我想在面临这样一个需求,根据关键字下图片
比如我在程序中输入  花,
程序就会帮我下载很多花的图片到指定目录。。
我该怎么写?求思路

解决方案 »

  1.   

    这个问题我以前做过类似,专门下载竞争对手或者自己的商品图片的。。就好比百度吧,我图片搜索的时候输入汽车;
    就会发一个url请求给百度
    http://image.baidu.com/i?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=map0000&sf=1&fmq=1382620149635_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=汽车后然响应返回给我一个html页面。。
    我根据这个html的代码去正则提取图片的链接,然后下载就好了。。
      

  2.   

    我擦,
    给你几个工具类自己组合吧。//获取html页面的
    public static String getStringByUrl(String urlstr){

    String result="";;
    try {
    InputStream is = getInputStreamByUrl(urlstr);
    result = IOHelper.fromIputStreamToString(is);
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return result;
    }
    public class IOHelper { /**
     * 把以一种编码格式的输入流转化为另一种编码格式的输出流
     * 
     * @return
     */
    public static void fromIsToOsByCode(InputStream is, OutputStream os,
    String incode, String outcode) {
    BufferedReader reader = null;
    BufferedWriter writer = null;
    try {
    reader = new BufferedReader(new InputStreamReader(is, incode));
    writer = new BufferedWriter(new OutputStreamWriter(os, outcode));
    String line;
    while ((line = reader.readLine()) != null) {
    writer.write(line + "\n");
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    reader.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    } /**
     * 以指定的格式来读取输入流
     */
    public static String readStrByCode(InputStream is, String code) {
    StringBuilder builder = new StringBuilder();
    BufferedReader reader = null; try {
    reader = new BufferedReader(new InputStreamReader(is, code));
    String line;
    while ((line = reader.readLine()) != null) {
    builder.append(line + "\n");
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    reader.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    return builder.toString();
    } /**
     * 输入InputStream流,返回字符串文字。
     * 
     * @param is
     * @return
     */
    public static String fromIputStreamToString(InputStream is) {
    if (is == null)
    return null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int i = -1;
    try {
    while ((i = is.read()) != -1) {
    baos.write(i);
    }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return baos.toString();
    } /**
     * 输入InputStream流和文件地址,返回成功与否。
     * 
     * @param is
     * @return
     */
    public static boolean fromIputStreamToFile(InputStream is,
    String outfilepath) {
    BufferedInputStream inBuff = null;
    BufferedOutputStream outBuff = null; try {
    // 新建文件输入流并对它进行缓冲
    inBuff = new BufferedInputStream(is); // 新建文件输出流并对它进行缓冲
    outBuff = new BufferedOutputStream(
    new FileOutputStream(outfilepath)); // 缓冲数组
    byte[] b = new byte[1024 * 5];
    int len;
    while ((len = inBuff.read(b)) != -1) {
    outBuff.write(b, 0, len);
    }
    // 刷新此缓冲的输出流
    outBuff.flush();
    } catch (Exception e) {
    return false;
    } finally {
    try {
    // 关闭流
    if (inBuff != null) inBuff.close();
    if (outBuff != null)
    outBuff.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } return true;
    } /**
     * 输入文件地址,返回inputStream流。
     * 
     * @param is
     * @return
     */
    public static InputStream fromFileToIputStream(String infilepath) {
    FileInputStream fis = null;
    try {
    fis = new FileInputStream(new File(infilepath));
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return fis;
    } public static InputStream fromStringToIputStream(String s) {
    if (s != null && !s.equals("")) {
    try { ByteArrayInputStream stringInputStream = new ByteArrayInputStream(
    s.getBytes());
    return stringInputStream;
    } catch (Exception e) { e.printStackTrace();
    }
    }
    return null;
    } /**
     * 把输入流转化成UTF-8格式的输入流
     * 
     * @param in
     * @param charset
     * @return
     * @throws Exception
     */
    // public static InputStream fromInputStreamToInputStreamInCharset(
    // InputStream in, String charset) throws Exception {
    // StringBuilder builder=new StringBuilder();
    // byte[] buffer = new byte[2048];
    // int len = -1;
    // while ((len = in.read(buffer)) != -1) {
    // builder.append(EncodingUtils.getString(buffer, 0, len, "UTF-8"));
    // }
    // return IOHelper.fromStringToIputStream(builder.toString());
    // } public static InputStream getInputStreamFromUrl(String urlstr) {
    try {
    InputStream is = null;
    HttpURLConnection conn = null;
    System.out.println("urlstr:" + urlstr);
    URL url = new URL(urlstr);
    conn = (HttpURLConnection) url.openConnection();
    if (conn.getResponseCode() == 200) {
    is = conn.getInputStream();
    return is;
    }
    } catch (Exception e) {
    System.out.println(e.toString());
    }
    return null;
    } public static void writerStrByCode(FileOutputStream os, String outcode,
    String str) {
    BufferedWriter writer = null;
    try {
    writer = new BufferedWriter(new OutputStreamWriter(os, outcode));
    writer.write(str);
    writer.flush();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }