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. hdu 6208(后缀自动机、或者AC自动机

    题意:给你n个字符串,问你是否存在一个字符串可以从中找到其他n-1个字符串. 思路:其实很简单,找到最长的那个字符串对他进行匹配,看是否能匹配到n-1个字符串. 可以用AC自动机或者后缀自动机做,但是 ...

  2. Jvm(jdk8)源码分析1-java命令启动流程详解

    JDK8加载源码分析 1.概述 现在大多数互联网公司都是使用java技术体系搭建自己的系统,所以对java开发工程师以及java系统架构师的需求非常的多,虽然普遍的要求都是需要熟悉各种java开发框架 ...

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

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

  4. thinkphp5 数据库和模型

    1.Db和模型的存在只是ThinkPHP5.0架构设计中的职责和定位不同,Db负责的只是数据(表)访问,模型负责的是业务数据和业务逻辑.2.Db和模型最明显的一个区别就是Db查询返回的数据类型为数组( ...

  5. 4. Father's Impact on a Child's Language Development 父亲对孩子语言发展的影响

    4. Father's Impact on a Child's Language Development 父亲对孩子语言发展的影响 (1)Im families with two working pa ...

  6. Win7 VS2015及MinGW环境编译矢量库agg-2.5和cairo-1.14.6

    书接上文,昨天装了MinGW,主要原因之一是要用到MSYS,所以顺手把FFMPEG又编译了一遍. 回到主题,其实我是想编译矢量库,因为最近要学习一些计算几何算法,所以找个方便的2D画图库就很重要. 说 ...

  7. s4-2 ALOHA 协议

    多路访问协议  随机访问协议(Random Access) 特点:站点争用信道,可能出现站点之间的冲突 典型的随机访问协议 • ALOHA协议 • CSMA协议 • CSMA/CD协议(以太网采 ...

  8. Spring boot 集成Dubbo简单版,准备工作,

    一.GitHub上找寻Dubbo资源 阿里巴巴在其GitHub上已经写好一个Github案例所以我们只要进入其Git上就可以看到和clone这个项目 二.阿里巴巴GitHub使用 https://gi ...

  9. ArcGIS的地理坐标系与大地坐标系

    一直以来,总有很多朋友针对地理坐标系.大地坐标系这两个概念吃不透.近日,在网上看到一篇文章介绍它们,非常喜欢.所以在此转发一下,希望能够对制图的朋友们有所帮助. 地理坐标:为球面坐标. 参考平面地是  ...

  10. XCode中安装cocoapods步骤

    Ruby是一种功能强大的面向对象的脚本语言 Gem是一个管理Ruby库和程序的标准包,它通过Ruby Gem(如 http://rubygems.org/ )源来查找.安装.升级和卸载软件包,非常的便 ...