JAVA代码实现下载解析网易云音乐到本地电脑的demo示例
生活随笔
收集整理的這篇文章主要介紹了
JAVA代码实现下载解析网易云音乐到本地电脑的demo示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.PC瀏覽器打開網易云音樂官網,搜索你想要下載的歌曲名稱或者歌手姓名
2.點開你要下載歌曲,進入播放頁,從瀏覽器地址欄里獲得歌曲的songID。
?
3.pom文件引入依賴
<!-- hutool工具類--><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.3.6</version></dependency>4. java實現代碼
import cn.hutool.core.map.MapUtil; import cn.hutool.http.HttpUtil; import lombok.extern.slf4j.Slf4j;import java.io.*; import java.net.URL; import java.net.URLConnection; import java.util.HashMap;@Slf4j public class NetEaseCloudMusic {//音樂保存目錄private static final String videoSavePath="d:/音樂/";public static void main(String[] args) {//替換歌曲ID和歌曲名稱getDownMusicURL("33497051","大城小愛");}/*** 方法描述: 獲得下載音樂連接** @param songID 歌曲ID* @param songName 歌曲名稱* @author tarzan* @date 2020年11月10日 10:33:40*/public static void getDownMusicURL(String songID,String songName) {String musicPath="http://music.163.com/song/media/outer/url?id="+songID;HashMap<String, String> headers = MapUtil.newHashMap();headers.put("User-Agent", "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Mobile Safari/537.36");String redirectUrl = HttpUtil.createGet(musicPath).addHeaders(headers).execute().header("Location");log.info("-----音頻真實地址鏈接-----\n"+redirectUrl);//下載音樂到本地downMusic(redirectUrl,songName,"網易云");}/*** 方法描述: 下載方法** @param httpUrl* @param title* @author tarzan* @date 2020年11月10日 10:34:09*/public static void downMusic(String httpUrl,String title,String source) {String fileAddress = videoSavePath+"/"+source+"/"+title+".mp3";int byteRead;try {URL url = new URL(httpUrl);//獲取鏈接URLConnection conn = url.openConnection();//輸入流InputStream inStream = conn.getInputStream();//封裝一個保存文件的路徑對象File fileSavePath = new File(fileAddress);//注:如果保存文件夾不存在,那么則創建該文件夾File fileParent = fileSavePath.getParentFile();if(!fileParent.exists()){fileParent.mkdirs();}//寫入文件FileOutputStream fs = new FileOutputStream(fileSavePath);byte[] buffer = new byte[1024];while ((byteRead = inStream.read(buffer)) != -1) {fs.write(buffer, 0, byteRead);}inStream.close();fs.close();log.info("\n-----音頻保存路徑-----\n"+fileSavePath.getAbsolutePath());} catch (FileNotFoundException e) {log.error(e.getMessage());} catch (IOException e) {log.error(e.getMessage());}}}5.java主方法運行,控制臺輸出
總結
以上是生活随笔為你收集整理的JAVA代码实现下载解析网易云音乐到本地电脑的demo示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (十一)Jmeter另一种调试工具 HT
- 下一篇: 初级图像混合——线性混合操作