Java 下载远程图片到本地

……………………
    public static void downloadImg(String imgUrl,String fileName) {
        final String DocumentName = fileName;
        final String DocumentPath = "imgs"+File.separator;
        final String DownloadURL = imgUrl;

        try {
            //自动创建文件夹
            File file = new File(DocumentPath);
            if (!file.exists() && !file.isDirectory()) {
                file.mkdirs();
            }
            //解析下载地址
            URL url = new URL(DownloadURL);
            URLConnection cnt = url.openConnection();
            InputStream inStream = cnt.getInputStream();
            FileOutputStream fos = new FileOutputStream(DocumentPath + DocumentName);
            int bytesum = 0;
            int byteread;
            byte[] buffer = new byte[1204];
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
                fos.write(buffer, 0, byteread);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
……………………

Java 下载远程图片到本地