吴亦凡,下载吴亦凡

package java2.pack1;

import com.linkedin.urls.Url;
import com.linkedin.urls.detection.UrlDetector;
import com.linkedin.urls.detection.UrlDetectorOptions;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by fhqplzj on 16-10-21 at 下午9:18.
 */
public class DownloadPictures {
    private static final String path = "/home/fhqplzj/IdeaProjects/DocumentClustering/out/pictures";
    private static int count = 0;

    private static void downloader(String str) throws IOException {
        URL url = new URL(str);
        URLConnection urlConnection = url.openConnection();
        InputStream inputStream = urlConnection.getInputStream();
        FileOutputStream fileOutputStream = new FileOutputStream(String.format("%s/pic%d", path, ++count));
        System.out.println(String.format("Downloading the %d picture", count));
        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            fileOutputStream.write(buffer, 0, len);
        }
        inputStream.close();
        fileOutputStream.close();
    }

    public static void main(String[] args) {
        String s = StringUtils.EMPTY;
        try {
            s = IOUtils.toString(new URL("https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=index&fr=&hs=2&sf=1&fmq=&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&word=%E5%90%B4%E4%BA%A6%E5%87%A1&oq=%E5%90%B4%E4%BA%A6%E5%87%A1&rsp=-1"), StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }
        List urls = new UrlDetector(s, UrlDetectorOptions.HTML).detect();
        List jpgs = urls.stream().filter(url -> url.toString().toLowerCase().endsWith("jpg")).collect(Collectors.toList());
        System.out.println(String.format("There are %d pictures to download", jpgs.size()));
        jpgs.forEach(url -> {
            try {
                downloader(url.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }
}

你可能感兴趣的:(java)