您的位置:首页 > 其它

如何保存远程服务器上的音乐文件

2006-10-11 10:50 357 查看

import java.io.BufferedReader;


import java.io.*;






public class reload...{




public staticvoid main(String[] args) throws IOException...{?


String s1 = "http://localhost:8080/20051211074416500.mp3";


java.io.InputStream is = null;


java.io.BufferedReader br = null;


BufferedInputStream bis = null;




BufferedOutputStream bos = null;








try...{


java.net.URL url = new java.net.URL(s1);


is = url.openStream();


bis = new java.io.BufferedInputStream(is);




}catch(Exception e)...{


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


}




File file = new File("D:/study/123.mp3");




if(!file.exists())...{


file.createNewFile();


}




try...{


bos = new BufferedOutputStream(new FileOutputStream(file));;


byte[] b = new byte[1024];


int c=0;




while((c=bis.read(b))>0)...{


bos.write(b,0,c);


}




}catch(Exception e)...{


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




}finally...{




try...{


bos.flush();


bis.close();




}catch(Exception e)...{


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


}


}


}


}

先把源文件贴出来省得忘了,其实就是个下载文件的问题,关键就是用好JAVA的流,这部分我还要好好看看,回头要详细地写一下。这里有个小误区,就是BufferedOutputStream 得3个方法,我开始的时候就各和BufferedReader弄混了,写了个

while(bis.read(b)!=-1) {
bos.write(b);

留作给自己的警告。

void
flush()

Flushes this buffered output stream.
void
, int, int)]write(byte[] b, int off, int len)

Writes
len
bytes from the specified byte array starting at offset
off
to this buffered output stream.
void
write(int b)

Writes the specified byte to this buffered output stream.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: