永发信息网

java中的UrlHttp下载出问题了。。高手来。

答案:2  悬赏:70  手机版
解决时间 2021-07-20 03:15

想用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("写入完毕!!");
}

}

最佳答案

package download;


import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;


public class Download {


private static int BUFFER_SIZE = 8096; // 缓冲区大小



public static void main(String[] args) throws Exception {


String urlStr = " http://cdn1-20.projectplaylist.com/e1/files/cdn7/mp3_new/2584535.mp3";
String fileName = "d:\\a.mp3";
Download down = new Download();
down.saveToFile(urlStr, fileName);
}



public void saveToFile(String urlStr, String fileName) throws IOException {
FileOutputStream fos = null;
BufferedInputStream bis = null;
HttpURLConnection httpUrl = null;
URL url = null;
byte[] buf = new byte[BUFFER_SIZE];
int size = 0;


// 建立链接
url = new URL(urlStr);
httpUrl = (HttpURLConnection) url.openConnection();
// 连接指定的资源
httpUrl.connect();
// 获取网络输入流
bis = new BufferedInputStream(httpUrl.getInputStream());
// 建立文件
fos = new FileOutputStream(fileName);


System.out.println("正在获取链接[" + urlStr + "]的内容...\n将其保存为文件[" + fileName
+ "]");
// 保存文件
while ((size = bis.read(buf)) != -1){
fos.write(buf, 0, size);
System.out.println("正在下载.......");
}
System.out.println("下载成功!");
fos.close();
bis.close();
httpUrl.disconnect();
}
}

全部回答

用BufferReader就不对了,既然得到了inputStream,直接写到一个FileOutputStream里就能解决问题,以下贴出部分代码

OutputStream os = new FileOutputStream("你的存放路径");

BufferedOutStream bos = new BufferedOutStream(os);

//uc对象是你上面的内容

BufferedInputStream bis = new BufferedInputStream(uc.getInputStream());

int bytesRead = 0;

byte[] buffer = new byte[8192]; while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {

System.out.println("已读取:"+bytesRead); bos.write(buffer, 0, bytesRead); }

我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
劲舞大概什么时候关闭。
哪里围巾便宜且质地好
怎么点亮soso帮我
关于名族团结的句子,关于团结的诗词
列表比较明清两朝加强专制统治的措施?
dnf下一个大礼包什么时候出
问问上的分是怎么得来的
QQ空间抢车位的车在哪啊,找不到啊
怎么我的QQ农场不显示好友列表了?
我在香港想玩童梦但是都是乱码怎么解决 拜托
谁会刷机!!!!
英语翻译当我需要你的时候,你总是不在我身边.
我刚刚买的AU衣服他怎么还没给我吖?急..
我家电脑为什么不能播放视频?高手告下
英语中怎么分主语,谓语等好多种语?主格,宾格
推荐资讯
这个礼拜的礼拜天我19 周岁生日
帮我设计一个网名(小波)??
DNF1-60升级路线
天天好头脑?
有哪位可以告诉我胆大自信是怎么练成的.
Vista收藏夹无法收藏
能给我张龙OL的千元财富卡吗
往返的近义词是什么,反的反义词是什么
年青人是不是都该学门技术
济南鲁能康桥在哪里?
学习股票,求系统课程
谁能送我一只QQ熊不 万分感谢 !!
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?