网络资源下载+抓包歌曲

利用IO流+网络编程知识对网络资源进行抓取

下载资源:

java代码:

public static void main(String[] args) throws Exception {
        //1、资源地址
        URL url = new URL("http://localhost:8080/zhangzhixi/hello.txt");
        //URL url = new URL("https://m10.music.126.net/20201205204413/c3e4902c67adf67826af9ea1f8e43edd/yyaac/obj/wonDkMOGw6XDiTHCmMOi/3063392415/2460/1384/ef87/7f8dce4bd486ff46401714248058dfb4.m4a");
        //2、打开http连接
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        //3、获取这个资源
        InputStream is = urlConnection.getInputStream();
        //4、将资源读取到本地
        FileOutputStream fos = new FileOutputStream("demo.txt");

        byte[] bytes = new byte[1024];
        int temp;
        while ((temp = is.read(bytes)) != -1) {
            fos.write(bytes, 0, temp);//写出数据
        }
        fos.close();
        is.close();
        urlConnection.disconnect();//断开连接
    }

为了方便演示我就在我的Tomcat上面新建一个web项目:

可以看到已经下载成功了、

网易云抓包下载音乐:

只需要替换URL链接地址 可以看到已经下载完毕了歌曲

原文地址:https://www.cnblogs.com/zhangzhixi/p/14091085.html