JAVA多线程下载
package com.jan.test; import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL; public class MultiDownTest {
public static void main(String[] args) throws IOException {
//下载地址
String path="http://dldir1.qq.com/qqfile/qq/TIM1.1.5/21175/TIM1.1.5.exe";
//下载线程数量
int threadNums=3;
URL url = new URL(path);
HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("GET");
con.setConnectTimeout(5000);
con.setReadTimeout(5000); if(con.getResponseCode()==200){
int length = con.getContentLength();
int size=length/threadNums;
File file=new File(DownUtil.getFileName(path));
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
randomAccessFile.setLength(length);
randomAccessFile.close();
for(int i=0;i<threadNums;i++){
int beginindex=i*size;
int endindex=(i+1)*size-1;
//如果为最后一个线程
if(i==threadNums){
endindex=length-1;
}
System.out.println("线程:"+i+"begin:"+beginindex+" end:"+endindex);
new DownloadThread(beginindex, endindex, i, path,threadNums).start();
}
}
con.disconnect();
}
}
package com.jan.test;
public class DownUtil {
public static String getFileName(String url){
return url.substring(url.lastIndexOf("/")+1);
}
}
package com.jan.test; import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; public class DownloadThread extends Thread {
private int beginIndex;
private int endIndex;
private int id;//线程id
private String path;//下载地址
private int threadNums;//线程数量
private static int hasFinsh;//下载完部分数
public DownloadThread(int beginIndex, int endIndex, int id,String path,int threadNums) {
super();
this.beginIndex = beginIndex;
this.endIndex = endIndex;
this.id = id;
this.path=path;
this.threadNums=threadNums;
} public void run() {
URL url=null;
HttpURLConnection con=null;
File file = new File(DownUtil.getFileName(path));
try {
//判断是否有进度临时文件
File hasFile=new File(id+".txt");
if(hasFile.exists()){
BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(hasFile)));
beginIndex=Integer.parseInt(reader.readLine());
reader.close();
}
int total=0;
url = new URL(path);
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
con= (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setConnectTimeout(5000);
con.setReadTimeout(5000);
con.setRequestProperty("Range", "bytes="+beginIndex+"-"+endIndex);
if(con.getResponseCode()==206){
InputStream inputStream = con.getInputStream();
randomAccessFile.seek(beginIndex);
//创建进度临时文件
File temp=new File(id+".txt");
RandomAccessFile tmpRandomAccessFile = new RandomAccessFile(temp, "rw");
byte[] buff=new byte[1024];
int len=0;
while((len=inputStream.read(buff))!=-1){
randomAccessFile.write(buff,0,len);
total+=len;
tmpRandomAccessFile.seek(0);
tmpRandomAccessFile.write(((beginIndex+total)+"").getBytes());
System.out.println("线程"+id+" 下载进度:"+(beginIndex+total));
}
randomAccessFile.close();
tmpRandomAccessFile.close();
//下载完毕,删除进度临时文件
synchronized (path) {
hasFinsh+=1;
if(hasFinsh==threadNums){
for(int i=0;i<threadNums;i++){
File f = new File(i+".txt");
f.delete();
}
}
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
con.disconnect();
}
}
}
JAVA多线程下载的更多相关文章
- JAVA多线程下载网络文件
JAVA多线程下载网络文件,开启多个线程,同时下载网络文件. 源码如下:(点击下载 MultiThreadDownload.java) import java.io.InputStream; im ...
- java多线程下载和断点续传
java多线程下载和断点续传,示例代码只实现了多线程,断点只做了介绍.但是实际测试结果不是很理想,不知道是哪里出了问题.所以贴上来请高手修正. [Java]代码 import java.io.File ...
- java 多线程下载功能
import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; impo ...
- java 多线程下载文件 以及URLConnection和HttpURLConnection的区别
使用 HttpURLConnection 实现多线程下载文件 注意GET大写//http public class MultiThreadDownload { public static void m ...
- Java多线程下载文件
package com.test.download; import java.io.File; import java.io.InputStream; import java.io.RandomA ...
- Java多线程下载器FileDownloader(支持断点续传、代理等功能)
前言 在我的任务清单中,很早就有了一个文件下载器,但一直忙着没空去写.最近刚好放假,便抽了些时间完成了下文中的这个下载器. 介绍 同样的,还是先上效果图吧. Jar包地址位于 FileDownload ...
- Java多线程下载初试
一.服务端/客户端代码的实现 服务端配置config @ConfigurationProperties("storage") public class StoragePropert ...
- Java多线程下载分析
为什么要多线程下载 俗话说要以终为始,那么我们首先要明确多线程下载的目标是什么,不外乎是为了更快的下载文件.那么问题来了,多线程下载文件相比于单线程是不是更快? 对于这个问题可以看下图. 横坐标是线程 ...
- java 多线程下载文件并实时计算下载百分比(断点续传)
多线程下载文件 多线程同时下载文件即:在同一时间内通过多个线程对同一个请求地址发起多个请求,将需要下载的数据分割成多个部分,同时下载,每个线程只负责下载其中的一部分,最后将每一个线程下载的部分组装起来 ...
- java多线程下载网络图片
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader ...
随机推荐
- 哈希与字典树与KMP
hash讲解 主要记录hash的公式: ; i<=len; i++) { Hash[i]=(Hash[i-]*)%mod)%mod; } 求hash的公式是这个,怎么求一小段的hash值呢? ; ...
- python学习 day21 (3月28日)----(抽象类 多态 nametuple dump)
不要因为走的路太久了,而忘记了为了什么而出发. 提前作准备了吗?把思维导图的东西做了吗? 和工作了几年的人,相比,是不是相同的水平,如果要写简历的话. 一边学习,一边复习. 小就是大,少就是多. 1. ...
- Time.fixedDeltaTime和Time.DeltaTime
在Update中使用 Time.deltaTime,获取到的是这一帧的时间,如果游戏卡,帧率低,那这个值就大.如果游戏流畅,帧率高,这个值就小,Time.deltaTime = 1.0f / 帧率 在 ...
- linux普通用户使用1024以下的端口(80)
linux对于非root权限用户不能使用1024以下的端口,对于一些服务,过高的权限,会带来一定的风险.那么对于低权限的用户如何对外开放1024以下的端口.我这里找到几种办法并且亲测可行 首先搭建环境 ...
- 在平台中使用JNDI 数据源
有些情况下为了系统的安全性考虑,可以将数据源配置成JNDI,在程序中只需要使用 容器的JNDI路径就可以了. 配置方法 1.在容器中配置数据源 <Context path="/&quo ...
- mysql (_mysql_exceptions.OperationalError) (1055, "Expression #1 of SELECT list is not in GROUP BY clause
sudo gedit /etc/mysql/my.cnf在打开的my.cnf文件中添加 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 保存,退 ...
- es6 字符串方法
1.字符串的新方法 includes() 包含属性 startsWith() 头部开始是否包含 endWith() 字符串是否在尾部 ========三个返回值都为布尔值 第二参数为数字 e ...
- HDU 5321 Beautiful Set (莫比乌斯反演 + 逆元 + 组合数学)
题意:给定一个 n 个数的集合,然后让你求两个值, 1.是将这个集合的数进行全排列后的每个区间的gcd之和. 2.是求这个集合的所有的子集的gcd乘以子集大小的和. 析:对于先求出len,len[i] ...
- readystatechange事件
IE为DOM文档中的某些部分readystatechange事件. 这个事件的目的是提供与文档或元素的加载状态有关的信息,但是这个事件的行为有时候也很难预测. 支持readystatechange事件 ...
- 20155205 2016-2017-2 《Java程序设计》第9周学习总结
20155205 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 第十六章 JDBC简介 厂商在实现JDBC驱动程序时,依方式可将驱动程序分为四种类型: JD ...