利用RandomAccessFile 实现多线程下载。

没事写个 多线程下载的例子,虽然项目中下载没用到 :oops: 。
我想知道,如果使用了多线程下载,当多用户同时对这一个资源进行下载的时候会怎么样。有木有这样的文推荐。这是个随手写的程序,如果问题,希望讨论下。 :D
class  --MyMutilDown

public class MyMutilDown {
/**
* 单线程的远程下载
*/
public void testOneTDown(String filePath, String url) {
try {
// 要写入的文件
File file = new File(filePath + getFileExtName(url));
FileWriter fWriter = new FileWriter(file);
URL ul = new URL(url);
URLConnection conn = ul.openConnection();
conn.setConnectTimeout(2000);// 请求超时时间
// int len = conn.getContentLength();
InputStream in = conn.getInputStream();
// byte[] by = new byte[1024];
int temp = 0;
while ((temp = in.read()) != -1) {
fWriter.write(temp);
}
fWriter.close();
in.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

/**
* 文件后缀名
*
* @param path
* @return
*/
public String getFileExtName(String path) {
return path.substring(path.lastIndexOf("."));
}
/**
* 测试多线程
*
* @param filePath
* 文件保存路径
* @param url
* url
* @param tnum
* 线程数量
*/
public void testMoreTDown(String filePath, String url, int tnum) {
try {
// 要写入的文件
final File file = new File(filePath + getFileExtName(url));
RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");// 建立随机访问
final URL ul = new URL(url);
HttpURLConnection conn = (HttpURLConnection) ul.openConnection();
conn.setConnectTimeout(2000);// 请求超时时间
conn.setRequestMethod("GET");
int len = conn.getContentLength();// 文件长度
accessFile.setLength(len);
accessFile.close();
final int block = (len + tnum - 1) / tnum;// 每个线程下载的快大小

for (int i = 0; i < tnum; i++) {
final int a = i;
new Thread(new Runnable() {
int start = block * a;// 开始位置
int end = block * (a + 1) - 1;// 结束位置
@Override
public void run() {
HttpURLConnection conn2 = null;
RandomAccessFile accessFile2 = null;
InputStream in = null;
try {
conn2 = (HttpURLConnection) ul.openConnection();
conn2.setConnectTimeout(2000);// 请求超时时间
conn2.setRequestMethod("GET");
// TODO Auto-generated method stub
conn2.setRequestProperty("Range", "bytes=" + start
+ "-" + end + "");// 设置一般请求属性 范围
in = conn2.getInputStream();
byte[] data = new byte[1024];
int len = 0;
accessFile2 = new RandomAccessFile(file, "rwd");
accessFile2.seek(start);

while ((len = in.read(data)) != -1) {
accessFile2.write(data, 0, len);
}
System.out.println("线程:" + a + "下载完成!");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {
try {
accessFile2.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
}).start();

}


} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
MyMutilDown mydown = new MyMutilDown();
String path = "http://static.ishare.down.sina.com.cn/5585234.txt?ssig=f7CrV3UL8%2B&Expires=1347724800&KID=sina,ishare&ip=1347592902,117.40.138.&fn=%E5%8E%9A%E9%BB%91%E5%AD%A6.TXT";
// mydown.downLoad(path, "D:\\aa", 1);
// mydown.testOneTDown("D:\\", path);
mydown.testMoreTDown("D:\\aa", path, 3);
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值