public class DownloadServer {

    private int threadCount = ;
private static String fileUrl = "https://dldir1.qq.com/qqtv/mac/TencentVideo_V2.2.1.42253.dmg";
// private static String fileUrl = "http://statics.garmentnet.cn/file/file_photo/show/news/5c3c055c5793d567739439.jpg"; private static ExecutorService executorService = new ThreadPoolExecutor(, , , TimeUnit.SECONDS, new LinkedBlockingQueue<>(), new Download(), new ThreadPoolExecutor.DiscardPolicy()); private static ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(, new Download()); private int getFileInfo() {
int count = ;
try {
URL url = new URL(fileUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
count = urlConnection.getContentLength();
System.out.println(count);
} catch (Exception e) {
e.printStackTrace();
}
return count;
} private void download(int fileSize) throws IOException {
File file = new File("d.dmg");
if (file.exists()) {
file.delete();
file.createNewFile();
}
int perSize = fileSize / threadCount;
int start;
int end;
for (int i = ; i < threadCount; i++) {
start = i * perSize;
if (i == threadCount - ) {
end = fileSize - ;
} else {
end = (i + ) * perSize - ;
} executorService.execute(new Download(fileUrl, file, start, end));
}
} public static void main(String[] args) {
DownloadServer downloadServer = new DownloadServer();
int size = downloadServer.getFileInfo();
scheduledExecutorService.scheduleAtFixedRate(new DownloadCount(size), , , TimeUnit.MILLISECONDS);
try {
downloadServer.download(size);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Download implements Runnable, ThreadFactory {

    private String fileUrl;
private File file;
private int start;
private int end;
public static AtomicInteger downloadFileSize = new AtomicInteger(0);
private static AtomicInteger threadNum = new AtomicInteger(0); public Download(){ } public Download(String fileUrl, File file, int start, int end) {
this.file = file;
this.start = start;
this.end = end;
this.fileUrl = fileUrl;
} @Override
public void run() {
try {
URL url = new URL(fileUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestProperty("Range", "bytes=" + start + "-" + end); InputStream inputStream = httpURLConnection.getInputStream();
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
randomAccessFile.seek(start);
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
randomAccessFile.write(bytes, 0, len);
downloadFileSize.addAndGet(len);
// System.out.println(Thread.currentThread().getName() + "-" + downloadFileSize.addAndGet(len));
}
randomAccessFile.close(); } catch (IOException e) {
e.printStackTrace();
}
} @Override
public Thread newThread(Runnable r) {
return new Thread(r, "Run-" + threadNum.incrementAndGet());
}
}
public class DownloadCount implements Runnable {

    private int fileSize;
public DownloadCount(int fileSize) {
this.fileSize = fileSize;
} @Override
public void run() {
if(fileSize != Download.downloadFileSize.get()) {
System.out.println(Download.downloadFileSize);
}
}
}
 
												

RandomAccessFile多线程下载的更多相关文章

  1. RandomAccessFile多线程下载、复制文件、超大文件读写

    最近在准备面试,翻了翻自己以前写的Demo,发现自己写了不少的工具包,今天整理了一下,分享给大家. 本文包含以下Demo: 1.常用方法测试 2.在文件中间插入一段新的数据 3.多线程下载文件 4.多 ...

  2. Java--使用多线程下载,断点续传技术原理(RandomAccessFile)

    一.基础知识 1.什么是线程?什么是进程?它们之间的关系? 可以参考之前的一篇文章:java核心知识点学习----并发和并行的区别,进程和线程的区别,如何创建线程和线程的四种状态,什么是线程计时器 简 ...

  3. 【Java EE 学习 22 下】【单线程下载】【单线程断点下载】【多线程下载】

    一.文件下载简述 1.使用浏览器从网页上下载文件,Servlet需要增加一些响应头信息 (1)response.setContentType("application/force-downl ...

  4. android 多线程下载 断点续传

    来源:网易云课堂Android极客班第八次作业练习 练习内容: 多线程 asyncTask handler 多线程下载的原理 首先获取到目标文件的大小,然后在磁盘上申请一块空间用于保存目标文件,接着把 ...

  5. 无废话Android之smartimageview使用、android多线程下载、显式意图激活另外一个activity,检查网络是否可用定位到网络的位置、隐式意图激活另外一个activity、隐式意图的配置,自定义隐式意图、在不同activity之间数据传递(5)

    1.smartimageview使用 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...

  6. Java 仿迅雷多线程下载

    package net.webjoy.jackluo.android_json; /** * 1.http Range "bytes="+ start+end * 2.Random ...

  7. android程序---->android多线程下载(一)

    多线程下载是加快下载速度的一种方式,通过开启多个线程去执行一个任务,可以使任务的执行速度变快.多线程的任务下载时常都会使用得到断点续传下载,就是我们在一次下载未结束时退出下载,第二次下载时会接着第一次 ...

  8. AccessRandomFile多线程下载文件

    写一个工具类 package com.pb.thread.demo; import java.io.File; import java.io.FileNotFoundException; import ...

  9. 通过HTTP协议实现多线程下载

    1. 基本原理,每条线程从文件不同的位置开始下载,最后合并出完整的数据. 2. 使用多线程下载的好处     下载速度快.为什么呢?很好理解,以往我是一条线程在服务器上下载.也就是说,对应在服务器上, ...

随机推荐

  1. vue 的进度条组件

    先看效果: 要想实现如上图的,进度跳效果,有两种方式,首先介绍第一种: 1.自己用 div 写一个,代码如下 <template> <div class="mfc-slid ...

  2. SpringBoot框架的使用

    什么是SpringBoot Spring Boot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目.大多数SpringBoot项目只需要很少的配置文件. SpringBoo ...

  3. 从零开始一起学习SLAM | 理解图优化,一步步带你看懂g2o代码

    首发于公众号:计算机视觉life 旗下知识星球「从零开始学习SLAM」 这可能是最清晰讲解g2o代码框架的文章 理解图优化,一步步带你看懂g2o框架 小白:师兄师兄,最近我在看SLAM的优化算法,有种 ...

  4. 关于npm Vue

    参考:http://www.runoob.com/w3cnote/vue2-start-coding.html 安装vue脚手架 npm install vue-cli -g 查看当前脚手架版本 np ...

  5. 使用msf对tomcat测试

    1.1 使用nmap命令对目标主机进行扫描.单击桌面空白处,右键菜单选择"在终端中打开". 1.2 在终端中输入命令"nmap –sV 192.168.1.3" ...

  6. Docker 推送镜像到hub.docker

    1.Docker镜像文件:lails.server.demo:1.0, 2.登录Docker:docker login[根据提示输入用户名/密码] 3.执行:docker push lails.ser ...

  7. C#窗体程序与sql sever 数据库链接

    一.所用工具 Visual Studio 2017和SQL Server Management Studio 2012 二.连接 打开SQL Server Management Studio 2012 ...

  8. Linux中安装Python2.7

    原文地址:http://www.jianshu.com/p/6425d18d3e47   安装依赖的库 yum -y install python-devel openssl openssl-deve ...

  9. python核心技术

    基本语法 Python的设计目标之一是让代码具备高度的可阅读性.它设计时尽量使用其它语言经常使用的标点符号和英文单字,让代码看起来整洁美观.它不像其他的静态语言如C.Pascal那样需要重复书写声明语 ...

  10. 用java 集合和映射实现文章的单词数目统计

    package 一_统计字母出现; import java.io.File; import java.io.FileNotFoundException; import java.util.HashMa ...