java 如何读写远程mp3文件
想要将一个远程的mp3文件(例如:www.baidu.com/a.mp3)读取保存到本地,在本地创建a.mp3,用java如何实现。谢谢了。...
想要将一个远程的mp3文件 (例如:www.baidu.com/a.mp3) 读取保存到本地,在本地创建a.mp3,用java如何实现。谢谢了。
展开
1个回答
推荐于2016-09-01
展开全部
//:URLReader.java ==================
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.net.URL;
public class URLReader {
public static void readAndSave(String url,String fileFullName)throws Exception{
System.out.println("Starting.");
URL u = new URL(url);
byte[] buffer = new byte[1024*8];
int read;
int ava = 0;
long start = System.currentTimeMillis();
BufferedInputStream bin = new BufferedInputStream(u.openStream());
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(fileFullName));
while((read=bin.read(buffer))>-1){
bout.write(buffer,0,read);
ava+=read;
long speed = ava/(System.currentTimeMillis()-start);
System.out.println("Download: "+ava+" byte(s)"+" avg speed: "+speed+" (kb/s)");
}
bout.flush();
bout.close();
System.out.println("Done. size:"+ava+" byte(s)");
}
public static void main(String[] args) throws Exception {
URLReader.readAndSave(
"http://51hr.28tel.com.tw/UploadFile/Record/20090531114140.mp3",
"d:/双人舞.mp3");
}
}
======================================================
输出显示:
Starting.
Download: 8192 byte(s) avg speed: 6 (kb/s)
Download: 8385 byte(s) avg speed: 6 (kb/s)
Download: 16577 byte(s) avg speed: 11 (kb/s)
Download: 17025 byte(s) avg speed: 11 (kb/s)
Download: 25217 byte(s) avg speed: 15 (kb/s)
Download: 25665 byte(s) avg speed: 16 (kb/s)
Download: 33857 byte(s) avg speed: 19 (kb/s)
Download: 34305 byte(s) avg speed: 19 (kb/s)
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.net.URL;
public class URLReader {
public static void readAndSave(String url,String fileFullName)throws Exception{
System.out.println("Starting.");
URL u = new URL(url);
byte[] buffer = new byte[1024*8];
int read;
int ava = 0;
long start = System.currentTimeMillis();
BufferedInputStream bin = new BufferedInputStream(u.openStream());
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(fileFullName));
while((read=bin.read(buffer))>-1){
bout.write(buffer,0,read);
ava+=read;
long speed = ava/(System.currentTimeMillis()-start);
System.out.println("Download: "+ava+" byte(s)"+" avg speed: "+speed+" (kb/s)");
}
bout.flush();
bout.close();
System.out.println("Done. size:"+ava+" byte(s)");
}
public static void main(String[] args) throws Exception {
URLReader.readAndSave(
"http://51hr.28tel.com.tw/UploadFile/Record/20090531114140.mp3",
"d:/双人舞.mp3");
}
}
======================================================
输出显示:
Starting.
Download: 8192 byte(s) avg speed: 6 (kb/s)
Download: 8385 byte(s) avg speed: 6 (kb/s)
Download: 16577 byte(s) avg speed: 11 (kb/s)
Download: 17025 byte(s) avg speed: 11 (kb/s)
Download: 25217 byte(s) avg speed: 15 (kb/s)
Download: 25665 byte(s) avg speed: 16 (kb/s)
Download: 33857 byte(s) avg speed: 19 (kb/s)
Download: 34305 byte(s) avg speed: 19 (kb/s)
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询