03-温故知新(下载网络资源)

public static void main(String[] args) throws Exception {

        /**
         * 第一种:
         * demo.jpg是要生成的文件名称
         * FileUtils.copyURLToFile(new URL("https://pic.liesio.com/2020/09/09/0370dbef0898b.jpg"), new File("demo.jpg"));
         */

        /**
         * 第二种:
         * demo.jpg是要生成的文件名称
         */
        URL url = new URL("https://pic.liesio.com/2020/09/09/0370dbef0898b.jpg");
        //打开连接
        URLConnection urlConnection = url.openConnection();
        InputStream is = urlConnection.getInputStream();
        FileOutputStream fos = new FileOutputStream("demo.jpg");
        int len = 0;
        byte[] bytes = new byte[1024];
        while ((len = is.read(bytes)) != -1) {
            fos.write(bytes, 0, len);
            fos.flush();
        }
        fos.close();
        is.close();
    }

 

posted @ 2020-12-26 10:26  Java小白的搬砖路  阅读(114)  评论(0编辑  收藏  举报