栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

java音频下载

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java音频下载

java 实现网易云音乐下载和播放

发布时间:2018-08-27 16:49,

浏览次数:872

, 标签:

java

不废话  直接上代码

首先 是下载工具类 根据url 下载文件

import java.io.BufferedInputStream; import java.io.BufferedReader; import

java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream;

import java.io.FileOutputStream; import java.io.IOException; import

java.io.InputStream; import java.io.InputStreamReader; import

java.io.OutputStream; import java.io.OutputStreamWriter; import

java.net.HttpURLConnection; import java.net.MalformedURLException; import

java.net.URL; import java.net.URLConnection; import java.util.Iterator; import

java.util.Map; public class

HttpConnectionUtil { @SuppressWarnings("finally") public static String

downloadFile(String urlPath, String downloadDir) { File file = null; String

path=null; try { // 统一资源 URL url = new URL(urlPath); // 连接类的父类,抽象类

URLConnection urlConnection = url.openConnection(); // http的连接类

HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection; //

设定请求的方法,默认是GET httpURLConnection.setRequestMethod("POST"); // 设置字符编码

httpURLConnection.setRequestProperty("Charset", "UTF-8"); // 打开到此 URL

引用的资源的通信链接(如果尚未建立这样的连接)。 httpURLConnection.connect(); // 文件大小 int fileLength =

httpURLConnection.getContentLength(); // 文件名 String filePathUrl =

httpURLConnection.getURL().getFile(); String fileFullName =

filePathUrl.substring(filePathUrl.lastIndexOf(File.separatorChar) + 1); //

System.out.println("file length---->" + fileLength); URLConnection con =

url.openConnection(); BufferedInputStream bin = new

BufferedInputStream(httpURLConnection.getInputStream()); path = downloadDir +

File.separatorChar + fileFullName; file = new File(path); if

(!file.getParentFile().exists()) { file.getParentFile().mkdirs(); }

OutputStream out = new FileOutputStream(file); int size = 0; int len = 0;

byte[] buf = new byte[1024]; while ((size = bin.read(buf)) != -1) { len +=

size; out.write(buf, 0, size); // 打印下载百分比 // System.out.println("下载了-------> "

+ len * 100 / fileLength + // "%n"); } bin.close(); out.close(); } catch

(MalformedURLException e) { // TODO Auto-generated catch block

e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch

block e.printStackTrace(); } finally { return path; } } }

然后是播放类

import java.io.BufferedInputStream; import java.io.FileInputStream; import

javazoom.jl.player.Player; public class MP3Player { private String filename;

private Player player; public MP3Player(String filename) { this.filename =

filename; } public void play() { try { BufferedInputStream buffer = new

BufferedInputStream(new FileInputStream(filename)); player = new

Player(buffer); player.play(); } catch (Exception e) { System.out.println(e); }

} public void stop() { try { player.close(); } catch (Exception e) {

System.out.println(e); } } }

重点来啦   这个 id 是搜索出来歌曲的url id

id=316277

public class Test { public static void main(String[] args) { // 下载文件测试 String

downloadFile =

HttpConnectionUtil.downloadFile("http://music.163.com/song/media/outer/url?id=316277",

"D:\cer"); System.out.println("下载返回的地址:"+downloadFile); try { //等待下载

Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated

catch block e.printStackTrace(); } MP3Player mp3 = new MP3Player(downloadFile);

//播放 mp3.play(); } }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/985178.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号