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 ...
随机推荐
- Java第3章笔记
if基本语法: if(条件){// 表达式 // 代码块 } eg: int a = 10; if(a > 1){ System.out.println("内容& ...
- 建库,建表,添加数据 SQL命令
create database ssm default character set utf8; use ssm; create table flower( id int(10) primary key ...
- js 判断 undefined,单选 以及下拉框选中状态
name = $(this).attr("title"); if(typeof(name) == 'undefined'){ alert(1); } typeof 函数 radio ...
- poj-3067(树状数组)
题目链接:传送门 题意:日本有东城m个城市,西城m个城市,东城与西城相互连线架桥,判断这些桥相交的次数. 思路:两个直线相交就是(x1-x2)*(y1-y2)<0,所以,对x,y进行排序,按照x ...
- flex布局中的主轴和侧轴的确定
1.主轴和侧轴是通过flex-direction确定的 如果flex-direction是row或者row-reverse,那么主轴就是justify-contain 如果flex-direction ...
- 使用express框架和mongoose在MongoDB查找数据
1.创建Schema var schema = new mongoose.Schema({ userName:{type:String,require:true}, age:{type:Number, ...
- Codeforces Round #524 (Div. 2) F. Katya and Segments Sets(主席树)
https://codeforces.com/contest/1080/problem/F 题意 有k个区间,区间的种类有n种,有m个询问(n,m<=1e5,k<=3e5),每次询问a,b ...
- js中对象继承的冒充方法
function Parent(name){ this.name = name; this.sayName = function(){ console.log(this.name); } } func ...
- Ajax地域选择demo
index.jsp只用于转发到Servlet获得省份数据再转发到province.jsp index.jsp <%@ page language="java" content ...
- VirtualBox安装增强工具时:Unable to install guest additions: unknown filesystem type 'iso9660'
解决方法: sudo apt-get install --reinstall linux-image-$(uname -r) 参考:http://askubuntu.com/questions/596 ...