public class DownFileSpliter extends Thread {
String dlURL;
long startPos;
long endPos;
int threadID;
boolean done = false;
public boolean stop = false;
RandomAccessFile file;
public DownFileSpliter(String dlURL, String saveAs, long nStart,
long nEnd, int id) throws IOException {
this.dlURL = dlURL;
this.startPos = nStart;
this.endPos = nEnd;
this.threadID = id;
file = new RandomAccessFile(saveAs, "rw");
file.seek(startPos);
} public void run() {
try {
long start = System.currentTimeMillis();
URL url = new URL(dlURL);
HttpURLConnection httpConnection = (HttpURLConnection) url
.openConnection();
String sProperty = "bytes=" + startPos + "-";
httpConnection.setRequestProperty("RANGE", sProperty);
httpConnection.setRequestProperty("Accept","image/gif,image/x-xbitmap,application/msword,*/*");
System.out.println("线程" + threadID + "下载文件、、、请等待");
InputStream input = httpConnection.getInputStream();
byte[] buf = new byte[1024];
int offset;
offset = (int) endPos - (int) startPos;
if (offset > 1024)
offset = 1024;
BufferedInputStream bis = new BufferedInputStream(httpConnection.getInputStream());
while (bis.read(buf,0,offset) > 0 && startPos < endPos)// 循环获取文件
{
offset = (int) endPos - (int) startPos;
if(offset>1024)
offset = 1024;
// System.out.println("threadID: " + threadID + " started: "+ startPos + "  offset: " + offset);
//Thread.sleep(2000);
file.write(buf, 0, offset);
if(stop){
System.out.println("线程:"+threadID+",被强制中止");
break;
}
startPos += offset;
}
System.out.println("线程" + threadID + "执行完毕!!");
System.out.println("线程"+threadID+",共用时:" + String.valueOf(System.currentTimeMillis() - start));
file.close();
input.close();
done = true;
} catch (Exception ex) {
ex.printStackTrace();
} }
}用以上线程下载网络文件,可以正常运行,而且下载下来的大小与用下载工具下载下来的内容大小一样,但是用以上程序下载时不能打开,而用工具下载的却可以打开。
特向高手请教关于多线程下载网络文件,请帮忙看看,以上程序出错在哪里,谢谢。

解决方案 »

  1.   

    首先,文件跟下载的一样大,这很正常,因为RandomAccessFile生成的文件是写在硬盘分配这样大的空间,然后再往里面写数据.
    其次,我有一点疑惑就是,你是不是对每个线程都新建了一个RandomAccessFile类啊?
      

  2.   

    while (bis.read(buf,0,offset) > 0 && startPos < endPos)// 循环获取文件
    {
    offset = (int) endPos - (int) startPos;
    if(offset>1024)
    offset = 1024;
    ====================
    换成这样:
    while ((offset = bis.read(buf)) != -1 && startPos < endPos)// 循环获取文件
    {
      

  3.   

    to hoverlees(好棒)是的,每个线程都新建了一个RandomAccessFile类,这样有问题吗?
      

  4.   

    to CrazyGou(阿狗)(...) 谢谢你,可以了,不过,还不知道,我原来的代码错在哪里,有空麻烦告之,谢谢。
      

  5.   

    不过,还是有点小问题,就是开5个线程与开单个线程的下载速度差不多,奇怪的是5个线程每个线程都有在下载内容,以下是打屏信息:
     线程总数: 5
    文件总大小:8343109
    线程:0下载范围:0--1668621
    线程:1下载范围:1668621--3337242
    线程:2下载范围:3337242--5005863
    线程:3下载范围:5005863--6674484
    线程:4下载范围:6674484--8343109
    线程 0启动
    线程 1启动
    线程 2启动
    线程 3启动
    线程 4启动
    线程0下载文件、、、请等待
    线程1下载文件、、、请等待
    线程2下载文件、、、请等待
    线程3下载文件、、、请等待
    线程4下载文件、、、请等待
    threadID: 0 started: 0  offset: 1024
    threadID: 3 started: 5005863  offset: 1024
    threadID: 4 started: 6674484  offset: 1024
    threadID: 2 started: 3337242  offset: 1024
    threadID: 1 started: 1668621  offset: 1024
    threadID: 0 started: 1024  offset: 1024
    threadID: 3 started: 5006887  offset: 1024
    threadID: 4 started: 6675508  offset: 1024
    threadID: 2 started: 3338266  offset: 1024
    threadID: 1 started: 1669645  offset: 1024
    threadID: 0 started: 2048  offset: 1024
    threadID: 3 started: 5007911  offset: 1024
    threadID: 4 started: 6676532  offset: 1024
    threadID: 2 started: 3339290  offset: 1024
    threadID: 1 started: 1670669  offset: 1024
    threadID: 0 started: 3072  offset: 1024
    threadID: 3 started: 5008935  offset: 1024
    threadID: 4 started: 6677556  offset: 1024
    threadID: 2 started: 3340314  offset: 1024
    threadID: 1 started: 1671693  offset: 1024
    threadID: 0 started: 4096  offset: 1024
    threadID: 3 started: 5009959  offset: 1024
    threadID: 4 started: 6678580  offset: 1024
    threadID: 2 started: 3341338  offset: 1024
    threadID: 1 started: 1672717  offset: 1024
    threadID: 0 started: 5120  offset: 1024
    threadID: 3 started: 5010983  offset: 1024
    threadID: 4 started: 6679604  offset: 1024
    threadID: 2 started: 3342362  offset: 1024
    threadID: 1 started: 1673741  offset: 1024
    threadID: 0 started: 6144  offset: 1024
    threadID: 3 started: 5012007  offset: 1024
    threadID: 4 started: 6680628  offset: 1024
    threadID: 2 started: 3343386  offset: 1024
    threadID: 1 started: 1674765  offset: 1024
      

  6.   

    你好,我几乎和你使用了同样的方法,我用的是socket传输,结果显示,多线程的效果很差,和单线程的速度没有什么区别。请问有谁知道为什么吗?
      

  7.   

    也在研究Java FTP 多线程download,虽然尚无结论,但是有两个思考点:
    1.下载的方式,是binary还是ASCII
    2.影响速度的问题,可以从socket的connection 的数量角度考虑
      

  8.   

    to mxkong 和Top rumeng1106(deng)
    你们单线程和多线程的速度差不多,可能是因为你们连接的服务器不支持多线程,也就是说虽然有多个线程在工作,但每一时刻真正传输的也只是一个线程。
    我最近在做一个基于ftp的多线程上传,用的就是IIS FTP服务器,它支持断点和多线程,所以用多线程的速度优势还是很明显的。