查看: 1447|回復: 0

[Java代碼] 命令行下載百度音樂

3萬

主題

3萬

帖子

10萬

積分

管理員

Rank: 9Rank: 9Rank: 9

積分
100197
發表于 2017-1-2 09:40:17
通過命令行下載百度音樂,多線程下載,固定保存在e盤
  1. package org.load.music;
  2. import java.io.FileOutputStream;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. import java.util.Scanner;
  8. import javax.xml.parsers.DocumentBuilder;
  9. import javax.xml.parsers.DocumentBuilderFactory;
  10. import org.w3c.dom.Document;
  11. import org.w3c.dom.Element;
  12. import org.w3c.dom.NodeList;
  13. // 命令行下載百度音樂
  14. public class BaiduMusic {
  15. public static void main(String[] args) {
  16. String musicName = null;
  17. String next = null;
  18. while(true) {
  19. System.out.println("輸入名稱:(輸入exit停止輸入)");
  20. musicName = new Scanner(System.in).next();
  21. if("exit".equalsIgnoreCase(musicName)) {
  22. break;
  23. }
  24. new Thread(new DownloadThread(musicName)).start();
  25. }
  26. }
  27. }
  28. class DownloadThread implements Runnable {
  29. private static final String URL = "http://box.zhangmen.baidu.com/x?op=12&count=1&title="; // 請求地址+音樂名稱+$(+歌手+$$)
  30. private String musicName = null;
  31. private String musicType = ".mp3";
  32. public DownloadThread(String musicName) {
  33. this.musicName = musicName;
  34. }
  35. @Override
  36. public void run() {
  37. try {
  38. this.download();
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. public void download() throws Exception {
  44. String path = this.parseMusic();
  45. URL url = new URL(path);
  46. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  47. conn.setConnectTimeout(5*1000); // 5s超時
  48. InputStream in = conn.getInputStream();
  49. OutputStream out = new FileOutputStream("E:\" + this.musicName + this.musicType);
  50. byte[] by = new byte[512];
  51. int len = 0;
  52. while(-1!=(len=in.read(by))) {
  53. out.write(by, 0, len);
  54. }
  55. out.flush();
  56. in.close();
  57. out.close();
  58. }
  59. public String parseMusic() throws Exception {
  60. // 解析url
  61. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  62. DocumentBuilder builder = factory.newDocumentBuilder();
  63. Document doc = builder.parse(URL + this.musicName + "$");
  64. NodeList encodes = doc.getElementsByTagName("encode"); // 得到encode節點
  65. Element encode = (Element) encodes.item(0); // 取得第一個encode節點
  66. String musicUrl = encode.getChildNodes().item(0).getNodeValue(); // 取得encode節點的TextNode
  67. String url = musicUrl.substring(0, musicUrl.lastIndexOf('/')) + "/"; // 解析出url
  68. // 解析名稱
  69. NodeList decodes = doc.getElementsByTagName("decode"); // 取得decode節點
  70. Element decode = (Element) decodes.item(0); // 取得第一個decode節點
  71. String musicName = decode.getFirstChild().getNodeValue(); // 解析出音樂名
  72. this.musicType = musicName.substring(musicName.indexOf('.'), musicName.indexOf('?')); // 解析出音樂類型
  73. return url + musicName;
  74. }
  75. }
復制代碼


回復

使用道具 舉報