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多线程下载的更多相关文章

  1. JAVA多线程下载网络文件

    JAVA多线程下载网络文件,开启多个线程,同时下载网络文件.   源码如下:(点击下载 MultiThreadDownload.java) import java.io.InputStream; im ...

  2. java多线程下载和断点续传

    java多线程下载和断点续传,示例代码只实现了多线程,断点只做了介绍.但是实际测试结果不是很理想,不知道是哪里出了问题.所以贴上来请高手修正. [Java]代码 import java.io.File ...

  3. java 多线程下载功能

    import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; impo ...

  4. java 多线程下载文件 以及URLConnection和HttpURLConnection的区别

    使用 HttpURLConnection 实现多线程下载文件 注意GET大写//http public class MultiThreadDownload { public static void m ...

  5. Java多线程下载文件

    package com.test.download;   import java.io.File; import java.io.InputStream; import java.io.RandomA ...

  6. Java多线程下载器FileDownloader(支持断点续传、代理等功能)

    前言 在我的任务清单中,很早就有了一个文件下载器,但一直忙着没空去写.最近刚好放假,便抽了些时间完成了下文中的这个下载器. 介绍 同样的,还是先上效果图吧. Jar包地址位于 FileDownload ...

  7. Java多线程下载初试

    一.服务端/客户端代码的实现 服务端配置config @ConfigurationProperties("storage") public class StoragePropert ...

  8. Java多线程下载分析

    为什么要多线程下载 俗话说要以终为始,那么我们首先要明确多线程下载的目标是什么,不外乎是为了更快的下载文件.那么问题来了,多线程下载文件相比于单线程是不是更快? 对于这个问题可以看下图. 横坐标是线程 ...

  9. java 多线程下载文件并实时计算下载百分比(断点续传)

    多线程下载文件 多线程同时下载文件即:在同一时间内通过多个线程对同一个请求地址发起多个请求,将需要下载的数据分割成多个部分,同时下载,每个线程只负责下载其中的一部分,最后将每一个线程下载的部分组装起来 ...

  10. java多线程下载网络图片

    import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader ...

随机推荐

  1. Python之生成器及内置函数篇4

    一.可迭代对象 #1.什么是迭代?:迭代是一个重复的过程,并且每次重复都是基于上一次的结果而来 #2.可迭代的对象:在python中,但凡内置有__iter__方法的对象,都是可迭代的对象 #3.迭代 ...

  2. sql在最后一行添加合计

    select nvl(sno,'合计') sno,sum(score) score from sc group by rollup(sno);

  3. [转]ajQuery的deferred对象详解

    来自:http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html 作者:  ...

  4. Java数据类型的转换

    Java数据类型,从小到大排序 byte ,short ,int ,long ,float, double,char 1.小数据类型转换大的数据类型,自动转换 int a = 3; double b ...

  5. 计算给定多项式在给定点X处的值

    //计算多项式求值 //计算多项式求值#include<iostream>#include<ctime>#include<cmath>using namespace ...

  6. vim 查找替换批量替换

    一.vi查找:    当你用vi打开一个文件后,因为文件太长,如何才能找到你所要查找的关键字呢?在vi里可没有菜单-〉查找, 不过没关系,你在命令模式下敲斜杆(/)这时在状态栏(也就是屏幕左下脚)就出 ...

  7. vba遗传算法之非一致性突变

    http://www.docin.com/p-959323141-f4.html Sub 非一致性变异() Dim totalGenerate As Integer, currentGenerate ...

  8. linux systemctl 常用用法简介

    主要介绍systemctl的几个功能如下: 1.查看某个服务的状态 2.关闭某个服务 3.开启某个服务 4.设置某个为开机自启动 5.关闭某个服务为开机不启动 6.查看所有开启启动的服务 1.查看某个 ...

  9. Ng第九课:神经网络的学习(Neural Networks: Learning)

    9.1  代价函数 9.2  反向传播算法 9.3  反向传播算法的直观理解 9.4  实现注意:展开参数 9.5  梯度检验 9.6  随机初始化 9.7  综合起来 9.8  自主驾驶 9.1   ...

  10. noip第7课资料