java 多线程下载文件

来自:http://www.oschina.net/code/snippet_725077_13424


import java.io.File;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

public class MultiThreadDownFile {
public static void main(String[] args) {
try {
String filePath = "http://dl_dir.qq.com/qqfile/qd/QQ2012Beta3_QQProtect2.8.exe";
int threadNum = 5;
new MultiThreadDown().down(filePath, threadNum);
} catch (Exception e) {
e.printStackTrace();
}
}
}

class MultiThreadDown extends Thread {

private int threadId;
private int block;
private URL url;
private File file;

public MultiThreadDown() {
}

public MultiThreadDown(int threadId, int block, URL url, File file) {
super();
this.threadId = threadId;
this.block = block;
this.url = url;
this.file = file;
}

public void down(String path, int threadNum) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
try {
if (conn.getResponseCode() == 200) {
int fileLength = conn.getContentLength();// Get the length of
// NetFile
System.out.println("网络文件大小:" + fileLength);
File file = new File(getFilename(path));
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
raf.setLength(fileLength);
raf.close();
// 计算每条线程的下载量
int block = fileLength % threadNum == 0 ? fileLength
/ threadNum : fileLength / threadNum + 1;
for (int threadId = 0; threadId < threadNum; threadId++) {
new MultiThreadDown(threadId, block, url, file).start();
System.out.println("线程" + threadId + "开始");
}
} else {
System.out.println("Connection failed.");
}
} catch (Exception e) {
System.out.println("连接失败.");
e.printStackTrace();
}
}

private String getFilename(String path) {
return path.substring(path.lastIndexOf("/") + 1);
}

@Override
public void run() {
super.run();
int start = threadId * block;// 计算线程下载的网络文件的文件位置
int end = (threadId + 1) * block - 1;// 线程的结束位置
try {
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
raf.seek(start);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Range", "bytes=" + start + "-" + end);
if (conn.getResponseCode() == 206) {
InputStream is = conn.getInputStream();
byte[] buf = new byte[1024];
int len = 0;
while ((len = is.read(buf)) != -1) {
raf.write(buf, 0, len);
}
raf.close();
is.close();
}
System.out.println("第" + threadId + "线程下载完毕");
} catch (Exception e) {
e.printStackTrace();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值