JAVA 通过url下载图片保存到本地

    //java 通过url下载图片保存到本地  urlString 图片链接地址  imgName 图片名称
    public static void download(String urlString, String imgName) throws Exception {
        // 构造URL
        URL url = new URL(urlString);
        // 打开连接
        URLConnection con = url.openConnection();
        // 输入流
        InputStream is = con.getInputStream();
        // 1K的数据缓冲
        byte[] bs = new byte[1024];
        // 读取到的数据长度
        int len;
        // 输出的文件流
        String filename = "D:\\本地路径/" + imgName + ".jpg";  //本地路径及图片名称
        File file = new File(filename);
        FileOutputStream os = new FileOutputStream(file, true);
        // 开始读取
        while ((len = is.read(bs)) != -1) {
            os.write(bs, 0, len);
        }
        System.out.println(imgName);
        // 完毕,关闭所有链接
        os.close();
        is.close();
    }