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. java框架之SpringBoot(10)-启动流程及自定义starter

    启动流程 直接从 SpringBoot 程序入口的 run 方法看起: public static ConfigurableApplicationContext run(Object source, ...

  2. ARIMA模型总结

    时间序列建模基本步骤 获取被观测系统时间序列数据: 对数据绘图,观测是否为平稳时间序列:对于非平稳时间序列要先进行d阶差分运算,化为平稳时间序列: 经过第二步处理,已经得到平稳时间序列.要对平稳时间序 ...

  3. SpringBoot项目启动时链接数据库很慢

    SpringBoot项目启动时链接数据库很慢 springboot项目在启动时候,如下图所示,链接数据库很慢 解决方法:在mysql 的配置文件中 配置 skip-name-resolve

  4. TheFatRat一般使用

    利用它生成后门 第一种通常情况下速度很快,较稳定,但免杀效果极差 推荐使用第二种 免杀效果好,如下图 第一种是最新的模块,较免杀效果极好.还可伪造图标 第二种是旧的

  5. java输入输出流(I/O)总结

    io流的 四个基本类 java.io包中包含了流式I/O所需要的所有类.在java.io包中有四个基本类:InputStream.OutputStream及Reader.Writer类,它们分别处理字 ...

  6. 怎样从外网访问内网Resin

    外网访问内网Resin 本地安装了Resin,只能在局域网内访问,怎样从外网也能访问本地Resin? 本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Resin 默认安装的Resin端口 ...

  7. 传值与传引用(C++)

    reference(引用) 是C++对C的一个扩充 int a; int &b = a;//声明b是一个整形的引用变量 C语言,函数的参数传递有2种形式:传值方式调用和传引用方式调用 传值方式 ...

  8. 【awk】提取文件第一列

    生信数据文件一般是按列分开的,如果我们只想简单的提取一列而不是费尽周折写个程序提取哪一列的话,awk作为一个非常好用的文档处理工具,我们现在来简单看一下他的一些功能: awk '{print $1}' ...

  9. Java连接数据库 #05# SQL与代码分离

    索引 读取html中的SQL语句 缺陷总结 在Java连接数据库 #04#里大概是这样放sql语句的: package org.sample.shop.db.queryrunner; import o ...

  10. layer(jQuery弹出层插件)

    弹窗alert:默认确定按钮+右上角关闭 top.layer.alert("请选择要删除的记录!",{shade: 0.3,offset:'250px'}); 弹窗alert:默认 ...