使用java自动下载网易云音乐

本文介绍如何使用网易云音乐的API服务,通过解析JSON文件获取歌曲ID及下载链接,并利用Java实现歌曲的自动下载。涉及Node.js、npm、JSON解析及HTTP请求等关键技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先向大家推荐一个github上的一个项目,是关于网易云音乐的一个APIservice,这个小的自动获取的程序就是建立在这个项目的基础之上的

项目地址
程序需要的基础

  1. 电脑上已将找好了node.js
  2. 使用npm install,或者也可以使用yarn install安装项目需要的依赖文件
  3. 下载解析json的jar包,这里我使用的是JSON.jar
    在这里插入图片描述
    出现类似这样的界面就是项目启动成功了,可以在浏览器中进行访问
    在这里插入图片描述

通过查看文档可以得到,得到音乐下载地址的步骤是

歌单详情
长方形
获取音乐id
获取音乐的url

我们得到的文件是一个json文件,于是我们就需要使用java的json库进行解析得到我们想要的文件

package RTTI;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class JSON {
    public static Map<String, Integer> getSongId(String fileName){
        Map<String, Integer> songMap = new HashMap<>();
        try {
            BufferedReader reader = new BufferedReader( new FileReader(fileName));
            String data = reader.readLine();
            JSONObject jo = new JSONObject(data);
            JSONObject jo1 = jo.getJSONObject("playlist");
            Boolean b = jo1.getBoolean("subscribed");
            System.out.println(b);
            JSONArray jo2 = jo1.getJSONArray("tracks");
            for(int i=0; i<jo2.length(); i++){
                JSONObject temp = (JSONObject) jo2.get(i);
                int id = temp.getInt("id");
                String name = temp.getString("name");
                songMap.put(name, id);
            }
            return songMap;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null ;
    }


    public static String getSongUrl(String fileName){
        String url = "" ;
        try {
            BufferedReader reader = new BufferedReader(new FileReader(fileName));
            String data = reader.readLine();
            JSONObject jo = new JSONObject(data);
            JSONArray ja = jo.getJSONArray("data");
            JSONObject jo1 = ja.getJSONObject(0);
            Object object = jo1.get("url");
            if(object instanceof String){
                System.out.println("ok");
                return (String)object;
            }else
                return "";
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return url ;
    }
}

上面的类是用来的解析json文件,得到自己需要的id的

package RTTI;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Set;

public class Download {
    public static void  downLoadFromUrl(String urlStr,String fileName,String savePath) throws IOException {
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        //设置超时间为3秒
        conn.setConnectTimeout(3*1000);
        //防止屏蔽程序抓取而返回403错误
        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

        //得到输入流
        InputStream inputStream = conn.getInputStream();
        //获取自己数组
        byte[] getData = readInputStream(inputStream);

        //文件保存位置
        File saveDir = new File(savePath);
        if(!saveDir.exists()){
            saveDir.mkdir();
        }
        File file = new File(saveDir+File.separator+fileName);
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(getData);
        if(fos!=null){
            fos.close();
        }
        if(inputStream!=null){
            inputStream.close();
        }
        System.out.println("info:"+url+" download success");

    }

    /**
     * 从输入流中获取字节数组
     * @param inputStream
     * @return
     * @throws IOException
     */
    public static  byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }

    public static void main(String[] args) {
        FileWriter fileWriter = null ;
        try {
            fileWriter = new FileWriter("d:\\url.txt");
        } catch (IOException e) {
            System.out.println("文件创建失败");
        }
        //通过第一次获得的是关于歌曲的name和对应的id
        Map<String, Integer> songMap = JSON.getSongId("d:\\id.json");
        Set name = songMap.keySet();
        Object[] names = name.toArray();
        for(int i=0; i<names.length; i++){
            //然后我们通过歌曲的id,循环得到歌曲对应的具体的json文件
            //其中就包含了我们想要的下载地址的url
            try{
                downLoadFromUrl("http://127.0.0.1:3000/song/url?id="+songMap.get(names[i]),
                        "temp.json","d:/");
            }catch (Exception e) {
                // TODO: handle exception
            }
            //通过第二次解析,我们的到的是所有歌曲的json文件中的url
            String url = JSON.getSongUrl("D:\\temp.json");
            if(url=="") continue;
            try {
                //进行保存歌曲文件
                downLoadFromUrl(url,  names[i]+".mp3", "e:/song");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

首先的得到了一个歌单的json文件,就是在主函数中的d:\id.json

上面这个类实现的功能是通过得到的url地址将文件保存到本地

最后贴上一张效果图
在这里插入图片描述

最后,如果什么问题或者疑问欢迎提出,大家一起讨论,一起进步

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

able陈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值