java中URL下载文件的问题。程序有问题,下载的mp3文件比原来的大,请问问题出在哪里?
classDownFile{publicstaticvoidmain(String[]args){URLurl=null;InputStreamis=null;FileO...
class DownFile {
public static void main(String[] args){
URL url = null;
InputStream is = null;
FileOutputStream fos = null;
try{
url = new URL("http://yinyueshiting.baidu.com/data2/music/121524984/12152327672000128.mp3?xcode=6de82a6019a0fd028ddb2ac4c63c2a556f8d94595cf48717");
URLConnection urlConn = url.openConnection();
System.out.println("文件大小: "+urlConn.getContentLength());
byte[] bfr = new byte [2048];
is = urlConn.getInputStream();
fos = new FileOutputStream("e:\\abc\\平凡之路.mp3");
while ((is.read(bfr)) != -1){
fos.write(bfr);
}
fos.flush();
System.out.println("下载完毕");
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if (is != null)
is.close();
if (fos != null)
fos.close();
}catch(Exception e){}
}
}
} 展开
public static void main(String[] args){
URL url = null;
InputStream is = null;
FileOutputStream fos = null;
try{
url = new URL("http://yinyueshiting.baidu.com/data2/music/121524984/12152327672000128.mp3?xcode=6de82a6019a0fd028ddb2ac4c63c2a556f8d94595cf48717");
URLConnection urlConn = url.openConnection();
System.out.println("文件大小: "+urlConn.getContentLength());
byte[] bfr = new byte [2048];
is = urlConn.getInputStream();
fos = new FileOutputStream("e:\\abc\\平凡之路.mp3");
while ((is.read(bfr)) != -1){
fos.write(bfr);
}
fos.flush();
System.out.println("下载完毕");
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if (is != null)
is.close();
if (fos != null)
fos.close();
}catch(Exception e){}
}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询