RandomAccessFile多线程下载
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多线程下载的更多相关文章
- RandomAccessFile多线程下载、复制文件、超大文件读写
最近在准备面试,翻了翻自己以前写的Demo,发现自己写了不少的工具包,今天整理了一下,分享给大家. 本文包含以下Demo: 1.常用方法测试 2.在文件中间插入一段新的数据 3.多线程下载文件 4.多 ...
- Java--使用多线程下载,断点续传技术原理(RandomAccessFile)
一.基础知识 1.什么是线程?什么是进程?它们之间的关系? 可以参考之前的一篇文章:java核心知识点学习----并发和并行的区别,进程和线程的区别,如何创建线程和线程的四种状态,什么是线程计时器 简 ...
- 【Java EE 学习 22 下】【单线程下载】【单线程断点下载】【多线程下载】
一.文件下载简述 1.使用浏览器从网页上下载文件,Servlet需要增加一些响应头信息 (1)response.setContentType("application/force-downl ...
- android 多线程下载 断点续传
来源:网易云课堂Android极客班第八次作业练习 练习内容: 多线程 asyncTask handler 多线程下载的原理 首先获取到目标文件的大小,然后在磁盘上申请一块空间用于保存目标文件,接着把 ...
- 无废话Android之smartimageview使用、android多线程下载、显式意图激活另外一个activity,检查网络是否可用定位到网络的位置、隐式意图激活另外一个activity、隐式意图的配置,自定义隐式意图、在不同activity之间数据传递(5)
1.smartimageview使用 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...
- Java 仿迅雷多线程下载
package net.webjoy.jackluo.android_json; /** * 1.http Range "bytes="+ start+end * 2.Random ...
- android程序---->android多线程下载(一)
多线程下载是加快下载速度的一种方式,通过开启多个线程去执行一个任务,可以使任务的执行速度变快.多线程的任务下载时常都会使用得到断点续传下载,就是我们在一次下载未结束时退出下载,第二次下载时会接着第一次 ...
- AccessRandomFile多线程下载文件
写一个工具类 package com.pb.thread.demo; import java.io.File; import java.io.FileNotFoundException; import ...
- 通过HTTP协议实现多线程下载
1. 基本原理,每条线程从文件不同的位置开始下载,最后合并出完整的数据. 2. 使用多线程下载的好处 下载速度快.为什么呢?很好理解,以往我是一条线程在服务器上下载.也就是说,对应在服务器上, ...
随机推荐
- mybatis plus XML文件如何使用多个where条件
网上搜到很多例子教你在mybatis plus使用XML文件来查询自定义的sql,但是给的例子都是给的只注解了一个where的例子.我最近在开发的一个项目中,因为涉及到了多表的复杂查询,需要在一个sq ...
- javaweb(3)之JSP&EL&JSTL
JSP(Java Server Page) 介绍 什么是 JSP ? 从用户角度看,JSP 就是一个网页. 从开发者角度看,它其实就是一个继承了 Servlet 的 java 类,所以可以直接说 JS ...
- python基础(13)-面向对象
类 类的定义和使用 # class Person: def __init__(self, name, age, gender): self.name = name self.age = age sel ...
- linux环境如何配置repo
(1)下载repo mkdir ~/bin curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/bin/repo ...
- 日志采集器windows客户端的配置释义
<Extension json> Module xm_json </Extension> <Extension charconv> Module xm_charco ...
- xxnet to google部署
1,github上下载xxnet项目 2,启动(点击 start) 3,确定启动好后访问 www.google.com (此时是可以访问的) 4,注册google账号或直接登陆 5,访问 https: ...
- [openjudge-动态规划]Maximum sum
题目描述 题目原文 描述 Given a set of n integers: A={a1, a2,-, an}, we define a function d(A) as below: d(A)=m ...
- 第七篇——Struts2的接收参数
Struts2的接收参数 1.使用Action的属性接收参数 2.使用Domain Model接收参数 3.使用ModelDriven接收参数 项目实例 1.项目结构 2.pom.xml <pr ...
- Centos7 升级 Ruby
Centos7通过yum 安装的Ruby 是2.0版本.版本较低,需要升级到2.5以上版本. #yum 安装ruby yum install ruby #查看ruby版本 ruby -v 以下开始升级 ...
- 使用docker部署ambari的若干要点
ambari部署各个组件 使用ambari进行部署时主要需要的组件包括: ambari-server: 主要部署的控制节点,负责控制agent进行部署. mysql: server存储的数据库.也支持 ...