请选择 进入手机版 | 继续访问电脑版
查看: 421|回复: 0

[Java代码] 命令行下载百度音乐

3万

主题

3万

帖子

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
100167
发表于 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. }
复制代码


回复

使用道具 举报