根据图片地址下载图片到本地

   public static void main(String[] args) {
        String urls = "http://rongcloud-web.qiniudn.com/docs_demo_rongcloud_logo.png";
        String path = "d:/pic.png";
        try {
            URL url = new URL(urls);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());
            FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;

            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 

posted @ 2022-09-21 14:40  安详的苦丁茶  阅读(26)  评论(0编辑  收藏  举报