1. 先按 Fn + F12, 再刷新頁面, 在 "All" 中找到 .m4a 文件, 然后復制其網址
2. 把URL網址放到 URL url = new URL("......")中, 其中省略號表示的就是復制的網址
1 package demo04; 2 3 import java.io.FileOutputStream; 4 import java.io.InputStream; 5 import java.net.HttpURLConnection; 6 import java.net.MalformedURLException; 7 import java.net.URL; 8 9 public class URLDown { 10 public static void main(String[] args) throws Exception { 11 // 1. 下載地址 12 URL url = new URL("https://m10.music.126.net/20210920173109/26145e0846905c08716d906d5b50a379/yyaac/obj/wonDkMOGw6XDiTHCmMOi/2987357185/ac0b/8002/9c78/c8a73efda5fb684102090605650ee030.m4a"); 13 14 // 2. 連接到這個資源 HTTP 15 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 16 17 InputStream inputStream = urlConnection.getInputStream(); 18 19 FileOutputStream fos = new FileOutputStream("love_story.m4a"); 20 21 byte[] buffer = new byte[1024]; 22 int len; 23 while ((len = inputStream.read(buffer)) != -1) { 24 fos.write(buffer, 0, len); // 寫出這個數據 25 } 26 27 28 // 關閉資源 29 fos.close(); 30 inputStream.close(); 31 urlConnection.disconnect(); 32 33 } 34 35 }
3. 下載完成后在當前文件夾下用本地或自己的音樂播放器打開即可.