HACKER

爬取图片链接(让简书更漂亮)

2017-11-03  本文已影响0人  关guan关guan

最近开了简书,准备在简书中找些有feel的图片作为插图,刚好也有目标网站的图片链接,但是由于有300+张,不方便一张张下载,所以写了个小工具,直接上java代码:

package com.guanstudy;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;

public class HelloWorld {
  public static void main(String[] args) throws UnsupportedEncodingException {
    for(int i=0;i<355;i++){
      downloadPicture("http://www.***.com/images_pub/pub_"+(i+1)+".jpg","/Users/user/Documents/pics/pub_"+(i+1)+".jpg"); 
    }
  }

  public static void downloadPicture(String urlString, String path) {
    URL url = null;
    try {
      url = new URL(urlString);
      DataInputStream dataInputStream = new DataInputStream(url.openStream());
      FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
      byte[] buffer = new byte[1024];
      int length;
      while ((length = dataInputStream.read(buffer)) > 0) {
        fileOutputStream.write(buffer, 0, length);
      }
      dataInputStream.close();
      fileOutputStream.close();
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
fox
上一篇下一篇

猜你喜欢

热点阅读