想用java实现下从网络上下载一首稻香MP3.写了下面的代码。。可是总是下载到中途就不动了。。但程序中显示还在读取;并没有抛异常。。地址资源没问题。用迅雷可以下载;把代码贴出来大家给瞧瞧哪里出错了??
import java.net.*;
import java.util.*;
import java.io.*;
public class Text {
public String getURl() {
URL url = null;
HttpURLConnection uc = null;
BufferedReader bf = null;
final int len = 0;
byte[] buff = new byte[len];
String message = "";
String temp = "";
int len1 = -1;
String urlStr = " http://cdn1-20.projectplaylist.com/e1/files/cdn7/mp3_new/2584535.mp3";
try {
url = new URL(urlStr);
System.out.println("开启连接之前" + new Date());
uc = (HttpURLConnection) url.openConnection();
bf = new BufferedReader(new InputStreamReader(uc.getInputStream()));
System.out.println("接受到读入流成功:" + new Date());
while ((temp = bf.readLine()) != null) {
System.out.println("正在下载.......");
System.out.println(new Date());
message += temp;
}
System.out.println("接受数据完毕!!!" + new Date());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bf != null) {
try {
bf.close();
} catch (IOException e) {
bf = null;
e.printStackTrace();
}
}
}
return message;
}
public void writeWebText(String context)
{
FileWriter fw = null;
try {
fw = new FileWriter("d:\\a.txt");
fw.write(context, 0, context.length());
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}finally
{
if (fw!=null) {
try {
fw.close();
} catch (IOException e) {
fw=null;
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Text t = new Text();
t.writeWebText(t.getURl());
System.out.println("写入完毕!!");
}
}