日期:2014-05-18  浏览次数:20719 次

为什么我从网上抓取的数会有总是
<%@   page   import= "java.io.* "%>
<%@   page   import= "java.io.Writer "%>
<%@   page   import= "java.net.* "%>
<%@   page   import= "java.util.Properties "%>
<%
String[]   picurl;
String[]   picsave;


String   s1   =   "http://www.ecds05.com/class3/qyk/image/1.mp3 ";  
//本地存放路径  
String   s2   =   "E:\\ "+System.currentTimeMillis()+ ".mp3 ";

URL   urlfile   =   null;  
HttpURLConnection   httpUrl   =   null;  
BufferedInputStream   bis   =   null;  
BufferedOutputStream   bos   =   null;  
File   f   =   new   File(s2);


try{  

urlfile   =   new   URL(s1);  
httpUrl   =   (HttpURLConnection)urlfile.openConnection();  
httpUrl.connect();  
bis   =   new   BufferedInputStream(httpUrl.getInputStream());  
}catch(Exception   e){  
out.println(e.toString());  
}

try{  
bos   =   new   BufferedOutputStream(new   FileOutputStream(f));;  
byte[]   b   =   new   byte[178];  
while(bis.read(b,0,b.length)!=-1)   {  
bos.write(b);  
}  
}catch(Exception   e){  
out.println(e.toString());  
}finally{  
try{  
bos.flush();  
bis.close();  
httpUrl.disconnect();  
}catch(Exception   e){  
out.println(e.toString());  
}  
}
%>

抓下的mp3文件播放的时候会像卡带的样子,为什么呀

------解决方案--------------------
你确定原来的mp3是不卡的吗?
------解决方案--------------------
把这段
byte[] b = new byte[178];
while(bis.read(b,0,b.length)!=-1) {
bos.write(b);
}

改成
byte[] b = new byte[178];
int count=0;
while((count=bis.read(b,0,b.length))!=-1) {
bos.write(b, 0, count);
}
试试!?
------解决方案--------------------
因为你在处理while(bis.read(b,0,b.length)!=-1) 时如果发生异常,那么就跳出循环了。

所以导致文件没下全。

把try catch重新写过,发生异常后继续读。。。直到读到为止