java 多线程下载 断点_Java 多线程断点下载文件
基起原根蒂根基理:哄骗URLConnection获取要下载文件的长度、头部等相干信息,并设置响应的头部信息。并且经由过程URLConnection获取输入流,将文件分成指定的块,每一块零丁开辟一个线程完成数据的读取、写入。经由过程输入流读取下载文件的信息,然后将读取的信息用RandomAccessFile随机写入到本地文件中。同时,每个线程写入的数据都文件指针也就是写入数据的长度,须要保存在一个姑且文件中。如许当本次下载没有完成的时辰,下次下载的时辰就从这个文件中读取上一次下载的文件长度,然后持续接着上一次的地位开端下载。并且将本次下载的长度写入到这个文件中。
一、下载文件信息类、实体
封装即将下载资料的信息
DownloadInfo.java
package com.hoo.entity;
/*** function: 下载文件信息类
*@authorhoojo
* @createDate 2011-9-21 下午05:14:58
* @file DownloadInfo.java
* @package com.hoo.entity
* @project MultiThreadDownLoad
* @bloghttp://blog.csdn.net/IBM_hoojo* @email hoojo_@126.com
*@version1.0*/
public class DownloadInfo {
//下载文件url private String url;
//下载文件名称 private String fileName;
//下载文件路径 private String filePath;
//分成几许段下载, 每一段用一个线程完成下载 private int splitter;
//下载文件默认保存路径 private final static String FILE_PATH = "C:/temp";
//默认分块数、线程数 private final static int SPLITTER_NUM = 5;
public DownloadInfo() {
super();
}
/***@paramurl 下载地址*/
public DownloadInfo(String url) {
this(url, null, null, SPLITTER_NUM);
}
/***@paramurl 下载地址url
*@paramsplitter 分成几许段或是几许个线程下载*/
public DownloadInfo(String url, int splitter) {
this(url, null, null, splitter);
}
/***
*@paramurl 下载地址
*@paramfileName 文件名称
*@paramfilePath 文件保存路径
*@paramsplitter 分成几许段或是几许个线程下载*/
public DownloadInfo(String url, String fileName, String filePath, int splitter) {
super();
if (url == null || "".equals(url)) {
throw new RuntimeException("url is not null!");
}
this.url = url;
this.fileName = (fileName == null || "".equals(fileName)) ? getFileName(url) : fileName;
this.filePath = (filePath == null || "".equals(filePath)) ? FILE_PATH : filePath;
this.splitter = (splitter < 1) ? SPLITTER_NUM : splitter;
}
/*** function: 经由过程url获得文件名称
*@authorhoojo
* @createDate 2011-9-30 下午05:00:00
*@paramurl
*@return*/
private String getFileName(String url) {
return url.substring(url.lastIndexOf("/") + 1, url.length());
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
if (url == null || "".equals(url)) {
throw new RuntimeException("url is not null!");
}
this.url = url;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = (fileName == null || "".equals(fileName)) ? getFileName(url) : fileName;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = (filePath == null || "".equals(filePath)) ? FILE_PATH : filePath;
}
public int getSplitter() {
return splitter;
}
public void setSplitter(int splitter) {
this.splitter = (splitter < 1) ? SPLITTER_NUM : splitter;
}
@Override
public String toString() {
return this.url + "#" + this.fileName + "#" + this.filePath + "#" + this.splitter;
}
}
二、随机写入一段文件
这个类主如果完成向本地的指定文件指针出开端写入文件,并返回当前写入文件的长度(文件指针)。这个类将被线程调用,文件被分成对应的块后,将被线程调用。每个线程都将会调用这个类完成文件的随机写入。
SaveItemFile.java
package com.hoo.download;
import java.io.IOException;
import java.io.RandomAccessFile;
/*** function: 写入文件、保存文件
*@authorhoojo
* @createDate 2011-9-21 下午05:44:02
* @file SaveItemFile.java
* @package com.hoo.download
* @project MultiThreadDownLoad
* @bloghttp://blog.csdn.net/IBM_hoojo* @email hoojo_@126.com
*@version1.0*/
public class SaveItemFile {
//存储文件 private RandomAccessFile itemFile;
public SaveItemFile() throws IOException {
this("", 0);
}
/***@paramname 文件路径、名称
*@parampos 写入点地位 position
*@throwsIOException*/
public SaveItemFile(String name, long pos) throws IOException {
itemFile = new RandomAccessFile(name, "rw");
//在指定的pos地位开端写入数据 itemFile.seek(pos);
}
/*** function: 同步办法写入文件
*@authorhoojo
* @createDate 2011-9-26 下午12:21:22
*@parambuff 缓冲数组
*@paramstart 肇端地位
*@paramlength 长度
*@return*/
public synchronized int write(byte[] buff, int start, int length) {
int i = -1;
try {
itemFile.write(buff, start, length);
i = length;
} catch (IOException e) {
e.printStackTrace();
}
return i;
}
public void close() throws IOException {
if (itemFile != null) {
itemFile.close();
}
}
}
三、单个线程下载文件
这个类主如果完成单个线程的文件下载,将经由过程URLConnection读取指定url的资料信息。然后用InputStream读取文件内容,然后调用调用SaveItemFile类,向本地写入当前要读取的块的内容。
DownloadFile.java
package com.hoo.download;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import com.hoo.util.LogUtils;
/*** function: 单线程下载文件
*@authorhoojo
* @createDate 2011-9-22 下午02:55:10
* @file DownloadFile.java
* @package com.hoo.download
* @project MultiThreadDownLoad
* @bloghttp://blog.csdn.net/IBM_hoojo* @email hoojo_@126.com
*@version1.0*/
public class DownloadFile extends Thread {
//下载文件url private String url;
//下载文件肇端地位 private long startPos;
//下载文件停止地位 private long endPos;
//线程id private int threadId;
//下载是否完成 private boolean isDownloadOver = false;
private SaveItemFile itemFile;
private static final int BUFF_LENGTH = 1024 * 8;
/***@paramurl 下载文件url
*@paramname 文件名称
*@paramstartPos 下载文件出发点
*@paramendPos 下载文件停止点
*@paramthreadId 线程id
*@throwsIOException*/
public DownloadFile(String url, String name, long startPos, long endPos, int threadId) throws IOException {
super();
this.url = url;
this.startPos = startPos;
this.endPos = endPos;
this.threadId = threadId;
//分块下载写入文件内容 this.itemFile = new SaveItemFile(name, startPos);
}
@Override
public void run() {
while (endPos > startPos && !isDownloadOver) {
try {
URL url = new URL(this.url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置连接超不时候为10000ms conn.setConnectTimeout(10000);
//设置读取数据超不时候为10000ms conn.setReadTimeout(10000);
setHeader(conn);
String property = "bytes=" + startPos + "-";
conn.setRequestProperty("RANGE", property);
//输出log信息 LogUtils.log("开端 " + threadId + ":" + property + endPos);
//printHeader(conn);//获取文件输入流,读取文件内容 InputStream is = conn.getInputStream();
byte[] buff = new byte[BUFF_LENGTH];
int length = -1;
LogUtils.log("#start#Thread: " + threadId + ", startPos: " + startPos + ", endPos: " + endPos);
while ((length = is.read(buff)) > 0 && startPos < endPos && !isDownloadOver) {
//写入文件内容,返回最后写入的长度 startPos += itemFile.write(buff, 0, length);
}
LogUtils.log("#over#Thread: " + threadId + ", startPos: " + startPos + ", endPos: " + endPos);
LogUtils.log("Thread " + threadId + " is execute over!");
this.isDownloadOver = true;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (itemFile != null) {
itemFile.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (endPos < startPos && !isDownloadOver) {
LogUtils.log("Thread " + threadId + " startPos > endPos, not need download file !");
this.isDownloadOver = true;
}
if (endPos == startPos && !isDownloadOver) {
LogUtils.log("Thread " + threadId + " startPos = endPos, not need download file !");
this.isDownloadOver = true;
}
}
/*** function: 打印下载文件头部信息
*@authorhoojo
* @createDate 2011-9-22 下午05:44:35
*@paramconn HttpURLConnection*/
public static void printHeader(URLConnection conn) {
int i = 1;
while (true) {
String header = conn.getHeaderFieldKey(i);
i++;
if (header != null) {
LogUtils.info(header + ":" + conn.getHeaderField(i));
} else {
break;
}
}
}
/*** function: 设置URLConnection的头部信息,假装恳求信息
*@authorhoojo
* @createDate 2011-9-28 下午05:29:43
*@paramcon*/
public static void setHeader(URLConnection conn) {
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092510 Ubuntu/8.04 (hardy) Firefox/3.0.3");
conn.setRequestProperty("Accept-Language", "en-us,en;q=0.7,zh-cn;q=0.3");
conn.setRequestProperty("Accept-Encoding", "utf-8");
conn.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
conn.setRequestProperty("Keep-Alive", "300");
conn.setRequestProperty("connnection", "keep-alive");
conn.setRequestProperty("If-Modified-Since", "Fri, 02 Jan 2009 17:00:05 GMT");
conn.setRequestProperty("If-None-Match", "\"1261d8-4290-df64d224\"");
conn.setRequestProperty("Cache-conntrol", "max-age=0");
conn.setRequestProperty("Referer", "http://www.baidu.com");
}
public boolean isDownloadOver() {
return isDownloadOver;
}
public long getStartPos() {
return startPos;
}
public long getEndPos() {
return endPos;
}
}
四、分段多线程写入文件内容
这个类主如果完成读取指定url资料的内容,获取该资料的长度。然后将该资料分成指定的块数,将每块的肇端下载地位、停止下载地位,分别保存在一个数组中。每块都零丁开辟一个自力线程开端下载。在开端下载之前,须要创建一个姑且文件,写入当前下载线程的开端下载指针地位和停止下载指针地位。
BatchDownloadFile.java
package com.hoo.download;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.hoo.entity.DownloadInfo;
import com.hoo.util.LogUtils;
/*** function: 分批量下载文件
*@authorhoojo
* @createDate 2011-9-22 下午05:51:54
* @file BatchDownloadFile.java
* @package com.hoo.download
* @project MultiThreadDownLoad
* @bloghttp://blog.csdn.net/IBM_hoojo* @email hoojo_@126.com
*@version1.0*/
public class BatchDownloadFile implements Runnable {
//下载文件信息 private DownloadInfo downloadInfo;
//一组开端下载地位 private long[] startPos;
//一组停止下载地位 private long[] endPos;
//休眠时候 private static final int SLEEP_SECONDS = 500;
//子线程下载 private DownloadFile[] fileItem;
//文件长度 private int length;
//是否第一个文件 private boolean first = true;
//是否停止下载 private boolean stop = false;
//姑且文件信息 private File tempFile;
public BatchDownloadFile(DownloadInfo downloadInfo) {
this.downloadInfo = downloadInfo;
String tempPath = this.downloadInfo.getFilePath() + File.separator + downloadInfo.getFileName() + ".position";
tempFile = new File(tempPath);
//若是存在读入点地位的文件 if (tempFile.exists()) {
first = false;
//就直接读取内容 try {
readPosInfo();
} catch (IOException e) {
e.printStackTrace();
}
} else {
//数组的长度就要分成几许段的数量 startPos = new long[downloadInfo.getSplitter()];
endPos = new long[downloadInfo.getSplitter()];
}
}
@Override
public void run() {
//初次下载,获取下载文件长度 if (first) {
length = this.getFileSize();//获取文件长度 if (length == -1) {
LogUtils.log("file length is know!");
stop = true;
} else if (length == -2) {
LogUtils.log("read file length is error!");
stop = true;
} else if (length > 0) {
/*** eg
* start: 1, 3, 5, 7, 9
* end: 3, 5, 7, 9, length*/
for (int i = 0, len = startPos.length; i < len; i++) {
int size = i * (length / len);
startPos[i] = size;
//设置最后一个停止点的地位 if (i == len - 1) {
endPos[i] = length;
} else {
size = (i + 1) * (length / len);
endPos[i] = size;
}
LogUtils.log("start-end Position[" + i + "]: " + startPos[i] + "-" + endPos[i]);
}
} else {
LogUtils.log("get file length is error, download is stop!");
stop = true;
}
}
//子线程开端下载 if (!stop) {
//创建单线程下载对象数组 fileItem = new DownloadFile[startPos.length];//startPos.length = downloadInfo.getSplitter() for (int i = 0; i < startPos.length; i++) {
try {
//创建指定个数单线程下载对象,每个线程自力完成指定块内容的下载 fileItem[i] = new DownloadFile(
downloadInfo.getUrl(),
this.downloadInfo.getFilePath() + File.separator + downloadInfo.getFileName(),
startPos[i], endPos[i], i
);
fileItem[i].start();//启动线程,开端下载 LogUtils.log("Thread: " + i + ", startPos: " + startPos[i] + ", endPos: " + endPos[i]);
} catch (IOException e) {
e.printStackTrace();
}
}
//轮回写入下载文件长度信息 while (!stop) {
try {
writePosInfo();
LogUtils.log("downloading……");
Thread.sleep(SLEEP_SECONDS);
stop = true;
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < startPos.length; i++) {
if (!fileItem[i].isDownloadOver()) {
stop = false;
break;
}
}
}
LogUtils.info("Download task is finished!");
}
}
/*** 将写入点数据保存在姑且文件中
*@authorhoojo
* @createDate 2011-9-23 下午05:25:37
*@throwsIOException*/
private void writePosInfo() throws IOException {
DataOutputStream dos = new DataOutputStream(new FileOutputStream(tempFile));
dos.writeInt(startPos.length);
for (int i = 0; i < startPos.length; i++) {
dos.writeLong(fileItem[i].getStartPos());
dos.writeLong(fileItem[i].getEndPos());
//LogUtils.info("[" + fileItem[i].getStartPos() + "#" + fileItem[i].getEndPos() + "]"); }
dos.close();
}
/*** function:读取写入点的地位信息
*@authorhoojo
* @createDate 2011-9-23 下午05:30:29
*@throwsIOException*/
private void readPosInfo() throws IOException {
DataInputStream dis = new DataInputStream(new FileInputStream(tempFile));
int startPosLength = dis.readInt();
startPos = new long[startPosLength];
endPos = new long[startPosLength];
for (int i = 0; i < startPosLength; i++) {
startPos[i] = dis.readLong();
endPos[i] = dis.readLong();
}
dis.close();
}
/*** function: 获取下载文件的长度
*@authorhoojo
* @createDate 2011-9-26 下午12:15:08
*@return*/
private int getFileSize() {
int fileLength = -1;
try {
URL url = new URL(this.downloadInfo.getUrl());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
DownloadFile.setHeader(conn);
int stateCode = conn.getResponseCode();
//断定http status是否为HTTP/1.1 206 Partial Content或者200 OK if (stateCode != HttpURLConnection.HTTP_OK && stateCode != HttpURLConnection.HTTP_PARTIAL) {
LogUtils.log("Error Code: " + stateCode);
return -2;
} else if (stateCode >= 400) {
LogUtils.log("Error Code: " + stateCode);
return -2;
} else {
//获取长度 fileLength = conn.getContentLength();
LogUtils.log("FileLength: " + fileLength);
}
//读取文件长度 /*for (int i = 1; ; i++) {
String header = conn.getHeaderFieldKey(i);
if (header != null) {
if ("Content-Length".equals(header)) {
fileLength = Integer.parseInt(conn.getHeaderField(i));
break;
}
} else {
break;
}
}*/
DownloadFile.printHeader(conn);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return fileLength;
}
}
五、对象类、测试类
日记对象类
LogUtils.java
package com.hoo.util;
/*** function: 日记对象类
*@authorhoojo
* @createDate 2011-9-21 下午05:21:27
* @file LogUtils.java
* @package com.hoo.util
* @project MultiThreadDownLoad
* @bloghttp://blog.csdn.net/IBM_hoojo* @email hoojo_@126.com
*@version1.0*/
public abstract class LogUtils {
public static void log(Object message) {
System.err.println(message);
}
public static void log(String message) {
System.err.println(message);
}
public static void log(int message) {
System.err.println(message);
}
public static void info(Object message) {
System.out.println(message);
}
public static void info(String message) {
System.out.println(message);
}
public static void info(int message) {
System.out.println(message);
}
}
下载对象类
DownloadUtils.java
package com.hoo.util;
import com.hoo.download.BatchDownloadFile;
import com.hoo.entity.DownloadInfo;
/*** function: 分块多线程下载对象类
*@authorhoojo
* @createDate 2011-9-28 下午05:22:18
* @file DownloadUtils.java
* @package com.hoo.util
* @project MultiThreadDownLoad
* @bloghttp://blog.csdn.net/IBM_hoojo* @email hoojo_@126.com
*@version1.0*/
public abstract class DownloadUtils {
public static void download(String url) {
DownloadInfo bean = new DownloadInfo(url);
LogUtils.info(bean);
BatchDownloadFile down = new BatchDownloadFile(bean);
new Thread(down).start();
}
public static void download(String url, int threadNum) {
DownloadInfo bean = new DownloadInfo(url, threadNum);
LogUtils.info(bean);
BatchDownloadFile down = new BatchDownloadFile(bean);
new Thread(down).start();
}
public static void download(String url, String fileName, String filePath, int threadNum) {
DownloadInfo bean = new DownloadInfo(url, fileName, filePath, threadNum);
LogUtils.info(bean);
BatchDownloadFile down = new BatchDownloadFile(bean);
new Thread(down).start();
}
}
下载测试类
TestDownloadMain.java
package com.hoo.test;
import com.hoo.util.DownloadUtils;
/*** function: 下载测试
*@authorhoojo
* @createDate 2011-9-23 下午05:49:46
* @file TestDownloadMain.java
* @package com.hoo.download
* @project MultiThreadDownLoad
* @bloghttp://blog.csdn.net/IBM_hoojo* @email hoojo_@126.com
*@version1.0*/
public class TestDownloadMain {
public static void main(String[] args) {
/*DownloadInfo bean = new DownloadInfo("http://i7.meishichina.com/Health/UploadFiles/201109/2011092116224363.jpg");
System.out.println(bean);
BatchDownloadFile down = new BatchDownloadFile(bean);
new Thread(down).start();*/
//DownloadUtils.download("http://i7.meishichina.com/Health/UploadFiles/201109/2011092116224363.jpg"); DownloadUtils.download("http://mp3.baidu.com/j?j=2&url=http%3A%2F%2Fzhangmenshiting2.baidu.com%2Fdata%2Fmusic%2F1669425%2F%25E9%2599%25B7%25E5%2585%25A5%25E7%2588%25B1%25E9%2587%258C%25E9%259D%25A2.mp3%3Fxcode%3D2ff36fb70737c816553396c56deab3f1", "aa.mp3", "c:/temp", 5);
}
}
多线程下载首要在第三部和第四部,其他的处所还是很好懂得。源码中供给响应的注释了,便于懂得。
java 多线程下载 断点_Java 多线程断点下载文件相关推荐
- java 多线程下载器_Java多线程的下载器(1)
实现了一个基于Java多线程的下载器,可提供的功能有: 1. 对文件使用多线程下载,并显示每时刻的下载速度. 2. 对多个下载进行管理,包括线程调度,内存管理等. 一:单个文件下载的管理 1. 单文件 ...
- java 多线程下载图片_java多线程实现下载图片并压缩
最近在做一个需求:从其他系统的ftp目录下载存储图片url的文件,然后读取文件中的url地址,根据地址下载图片后按天压缩成一个包,平均一个地址文件中包含4000个地址左右,也就是说一个文件扫描后需要下 ...
- java上传加密_Java上传下载文件并实现加密解密
使用 Jersey 服务器实现上传,使用 HTTP 请求实现下载 引入依赖 在 pom.xml 中添加 Jersey 相关依赖 com.sun.jersey jersey-client 1.18.1 ...
- java线程代码实现_Java 多线程代码实现讲解
作为一个完全面向对象的语言,Java提供了类 java.lang.Thread 来方便多线程编程,这个类提供了大量的方法来方便我们控制自己的各个线程.那么如何提供给 Java 我们要线程执行的代码呢? ...
- java 锁旗标_Java多线程
Java多线程 1. 多线程存在的意义 多线程最大的作用就是能够创建"灵活响应"的桌面程序,而编写多线程最大的困难就是不同线程之间共享资源的问题,要使这些资源不会同时被多个线程访问 ...
- java线程怎么用_Java多线程基本使用
一.概念 1.进程 1.1进程:是一个正在进行中的程序,每一个进程执行都有一个执行顺序,该顺序是一个执行路径,或者叫一个控制单元. 1.2线程:就是进程中一个独立的控制单元,线程在控制着进程的执行,一 ...
- java获取word图片_java代码获取word文件中的图片
1:需求 在线打开word文件后,用户插入图片.然后保存图片到服务器 2:方案 用pageoffice 在线打开文档的功能和提交数据的功能实现 3:核心代码 4:实现过程 (1)因为要获取数据区域的数 ...
- java 按字节读入_Java按字节读取文件
[题外话]今天学习了Object-c.通过看书发现,Object-c跟大多数面向对象的语言一样,是很有意思的语言.里面的语法跟Java还有C++,形式上有些差异.至于我为什么要学OC,这个是因为工作需 ...
- java object取数据_java使用ObjectInputStream从文件中读取对象
import java.io.EOFException; import java.io.FileInputStream; import java.io.FileNotFoundException; i ...
最新文章
- 数据分析 从零开始到实战,Pandas读写CSV数据
- SAP PM信息系统
- 机器学习算法加强——数据清洗
- 【视频课】图像分割重磅上新-人像抠图(Matting)实战
- python开多少进程合适_用了python多进程,我跑程序花费的时间缩短了4倍
- http://www.myeclipseide.com/ 官网打不开的问题(转)
- AnkhSVN 1.0.2778 简体中文版发布
- PDH-SDH光端机指示灯具体含义介绍
- matlab中关于程序运行的快捷键
- cdoj 1252 24点游戏 dfs
- EXTRONICS推出IRFID500便携式UHF RFID读写器
- MongoError: E11000 duplicate key error collection: blog.users index: email_1 dup key
- Android之使用枚举利弊及替代方案
- linux下部署tomcat没起成功,linux下部署tomcat 上线jpress博客系统
- 浅谈unicode字符集及编码方式
- 2013级C++第15周项目——二维数组
- 计算机专业自主招生有哪些学校,自主招生的学校类型有哪些
- Flutter免费视频第二季-常用组件讲解
- 从0开始安装苹果cms及其资源采集和页面部分代码
- 请大家访问我的MSN空间(http://pal3boy.spaces.live.com)
热门文章
- Win7或是过渡品Win8前瞻消息全预览
- 利用taichi写一个最简单的SPH(光滑粒子动力学)
- Go语言(下载、安装、环境配置、GoLand编译器安装、编写HelloWorld)
- Codeforces Round #501 (Div. 3) F(动态规划)
- USACO 1月 2020-2021 January Contest Silver 题解
- c语言键盘中断扫描码,用51单片机中断编写的4x4键盘程序
- 利用matplotlib绘制条形图,直观呈现2019年电影票房数据
- 2020CVPR-目标分割-SPNet-Strip Pooling Rethinking Spatial Pooling for Scene Parsing
- 如何高性能的给UIImageView加个圆角?(不准说layer.cornerRadius!)
- UVa:12086 Potentiometers