编程语言
首页 > 编程语言> > java 下载网上图片

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();
    }

 

标签:java,String,newFileName,urlStr,split,file,下载,图片
来源: https://www.cnblogs.com/luohaonan/p/13038091.html