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. 使用多线程下载的好处 下载速度快.为什么呢?很好理解,以往我是一条线程在服务器上下载.也就是说,对应在服务器上, ...
随机推荐
- python日期加减法操作
对日期的一些操作: 对日期的一些操作: 1 #日期转化为字符串并得到指定(或系统日期)n天后的日期--@Eillot 2 def dataTimeToString(dsNow=ReservationT ...
- TCP/IP协议 模型
OSI的来源 OSI(Open System Interconnect),即开放式系统互联. 一般都叫OSI参考模型,是ISO(国际标准化组织)组织在1985年研究的网络互连模型. ISO为了更好的 ...
- javaweb(2)之Servlet入门
Hello Servlet 方式一 1.新建 web 工程,编写一个类,实现 javax.servlet.Servlet 接口: package com.zze.servlet; import jav ...
- python框架之Django(4)-视图&路由
视图 负责接收请求和返回响应 FBV和CBV FBV FBV(function base views) 就是在视图里使用函数处理请求. from django.conf.urls import url ...
- mescroll在vue中的应用
1.npm install --save mescroll.js 2. <template> <div> <!--全部--> <mescroll-vue re ...
- IdentityServer4授权和认证集成Identity和profile
identiyt的使用可以看之前的文章:https://www.cnblogs.com/nsky/p/10323415.html 之前的ids4授权服务器都是用的in-men方式把数据添加到内存, 现 ...
- C#设计模式(8)——桥接模式(Bridge Pattern)(转)
一.引言 这里以电视遥控器的一个例子来引出桥接模式解决的问题,首先,我们每个牌子的电视机都有一个遥控器,此时我们能想到的一个设计是——把遥控器做为一个抽象类,抽象类中提供遥控器的所有实现,其他具体电视 ...
- Lambda表达式详解(例子详解)(转自:http://blog.csdn.net/damon316/article/details/51734661)
Lambda表达式详解(例子详解) lambda简介 lambda运算符:所有的lambda表达式都是用新的lambda运算符 " => ",可以叫他,“转到”或者 ...
- Spring Boot 的 application.properties
更改默认端口:8080 server.port = 8081 更改context-path :/server.context-path = /springboot #server.address= # ...
- pypi上传问题
pypi上传过程中报错403 windows 解决办法: 1.建一个新的记事本编辑内容 [distutils]index-servers = pypi [pypi]repository:https:/ ...