java 下载网上图片

webmagic抓取某网站的图片,需要保存图片。

 

import java.io.*;
import java.net.URL;
import java.net.URLConnection;

public class FileDownloader {

    public static void download(String urlStr, String destDir, String... newFileName) throws IOException {

        if (null == urlStr || "".equalsIgnoreCase(urlStr)) {
            return;
        }

        URL url = new URL(urlStr);
        URLConnection connection = url.openConnection();
        connection.setConnectTimeout(5000);
        InputStream in = connection.getInputStream();
        byte[] bytes = new byte[1024];
        int len;
        File file = new File(destDir);
        if (!file.exists()) {
            file.mkdirs();
        }

        String[] split = urlStr.split("/");
        String fileName = split[split.length - 1];

        if (newFileName != null && newFileName.length > 0) {
            fileName = newFileName[0] + "." + fileName.split("\\.")[1];
        }

        OutputStream out = new FileOutputStream(file.getPath() + "/" + fileName);
        while ((len = in.read(bytes)) != -1) {
            out.write(bytes, 0, len);
        }
        out.close();
        in.close();
    }

 

上一篇:chrome restlet client插件安装


下一篇:Python重命名文件夹下的文件