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. html--笔记day03

    html--笔记day03 1.结构标记 1.<header>元素 <header></header> ==> <div id="header ...

  2. thinkphp5 数据库和模型

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

  3. yii2 控制器渲染

    render() : 渲染一个 视图名并使用一个 布局返回到渲染结果. renderPartial() : 渲染一个 视图名并且不使用布局. renderAjax() : 渲染一个 视图名并且不使用布 ...

  4. spring+springMVC+mybatis+maven+mysql环境搭建(二)

    上一篇整合了spring+mybatis,基本上还不是web工程,接下来接入springMVC,Let's go! 一.工程转换成Web工程 首先右击项目-->properties-->p ...

  5. 安装配置ftp服务器

    1.安装ftp服务 检查是否安装vsftpd rpm -qa|grep vsftpd 安装 vsftpd yum -y install vsftpd 2.配置 编辑 文件 /etc/vsftpd/vs ...

  6. 在vue或者react中使用express框架

    在react 或者 vue项目中使用express框架 1.创建vue或者 react 项目 2.在项目中创建server文件夹,创建server.js //require()方法引入express模 ...

  7. Codeforces Round #514 (Div. 2) E. Split the Tree(倍增+贪心)

    https://codeforces.com/contest/1059/problem/E 题意 给出一棵树,每个点都有一个权值,要求你找出最少条链,保证每个点都属于一条链,而且每条链不超过L个点 和 ...

  8. IE与非IE window.onload调用

    IEwin.attachEvent('onload', function(){ });非IEwin.onload=function(){}; if(navigator.appName == " ...

  9. IntelliJ IDEA 2017版 spring-boot 2.0.3 部署war包项目和jar包项目

    1.建立项目 Java Controller package com.springboot.jsp.controller; import org.springframework.stereotype. ...

  10. Manacher算法,最长回文串

    给你10000长度字符串,然你求最长回文字串,输出长度,暴力算法肯定超时 #include <iostream> #include <string> #include < ...