java线程下载文件_使用多线程在Java下载文件

我正在做一个类似IDM的下载器,我已经读到了这篇关于它的文章。我已经实现了我的第一步代码。

下面是Downloader类的代码:package download.manager;

import java.io.FileOutputStream;

import java.io.IOException;

import java.net.MalformedURLException;

import java.net.URL;

import java.nio.channels.ReadableByteChannel;

import java.util.logging.Level;

import java.util.logging.Logger;

public class Downloader implements Runnable{

private String url;

private int num;

private long start;

private long end;

ReadableByteChannel rbc;

public Downloader(String url, int num, long start, long end, ReadableByteChannel rbc) {

this.url = url;

this.num = num;

this.start = start;

this.end = end;

this.rbc = rbc;

}

@Override

public void run() {

download();

}

private void download(){

try {

System.out.println(num + " is executing");

URL file = new URL(url);

FileOutputStream stream = new FileOutputStream("tmp"+num);

stream.getChannel().transferFrom(rbc, start, end);

} catch (MalformedURLException ex) {

Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);

} catch (IOException ex) {

Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);

}

}

}

这就是我的主要功能:package download.manager;

import java.io.IOException;

import java.net.MalformedURLException;

import java.net.URL;

import java.nio.channels.Channels;

import java.nio.channels.ReadableByteChannel;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.TimeUnit;

import java.util.logging.Level;

import java.util.logging.Logger;

/**

*

* @author Behzad

*/

public class DownloadManager {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

URL file = null;

ReadableByteChannel rbc = null;

try {

String url = "http://dl1.video.varzesh3.com/video/clip93/12/video/havashi/top5_save_derby_dortmond.mp4";

file = new URL(url);

rbc = Channels.newChannel(file.openStream());

int size = file.openConnection().getContentLength();

ExecutorService pool = Executors.newFixedThreadPool(4);

int partSize = size / 4;

pool.submit(new Downloader(url, 1, 0, partSize, rbc));

pool.submit(new Downloader(url, 2, partSize, partSize, rbc));

pool.submit(new Downloader(url, 3, 2 * partSize, partSize, rbc));

pool.submit(new Downloader(url, 4, 3 * partSize, partSize, rbc));

pool.shutdown();

pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);

} catch (MalformedURLException | InterruptedException ex) {

Logger.getLogger(DownloadManager.class.getName()).log(Level.SEVERE, null, ex);

} catch (IOException ex) {

Logger.getLogger(DownloadManager.class.getName()).log(Level.SEVERE, null, ex);

}

}

}

但是当我运行这段代码时,下载程序只下载文件的第一部分。如图所示

7ef23ff1afc7494153f241a9d96e9010.png

我该为这个怎么办?

下面是更新的下载方法:private void download(){

try {

System.out.println(num + " is executing");

URL file = new URL(url);

ReadableByteChannel rbc = Channels.newChannel(file.openStream());

FileOutputStream stream = new FileOutputStream("tmp"+num);

stream.getChannel().transferFrom(rbc, start, end);

} catch (MalformedURLException ex) {

Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);

} catch (IOException ex) {

Logger.getLogger(Downloader.class.getName()).log(Level.SEVERE, null, ex);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值