使用java实现http多线程断点下载文件(一)
程序员文章站
2023-11-05 22:13:46
基本原理:利用urlconnection获取要下载文件的长度、头部等相关信息,并设置响应的头部信息。并且通过urlconnection获取输入流,将文件分成指定的块,每一块...
基本原理:利用urlconnection获取要下载文件的长度、头部等相关信息,并设置响应的头部信息。并且通过urlconnection获取输入流,将文件分成指定的块,每一块单独开辟一个线程完成数据的读取、写入。通过输入流读取下载文件的信息,然后将读取的信息用randomaccessfile随机写入到本地文件中。同时,每个线程写入的数据都文件指针也就是写入数据的长度,需要保存在一个临时文件中。这样当本次下载没有完成的时候,下次下载的时候就从这个文件中读取上一次下载的文件长度,然后继续接着上一次的位置开始下载。并且将本次下载的长度写入到这个文件中。
一、下载文件信息类、实体
封装即将下载资源的信息
复制代码 代码如下:
package com.hoo.entity;
/**
* <b>function:</b> 下载文件信息类
* @author hoojo
* @createdate 2011-9-21 下午05:14:58
* @file downloadinfo.java
* @package com.hoo.entity
* @project multithreaddownload
* @blog http://blog.csdn.net/ibm_hoojo
* @email hoojo_@126.com
* @version 1.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();
}
/**
* @param url 下载地址
*/
public downloadinfo(string url) {
this(url, null, null, splitter_num);
}
/**
* @param url 下载地址url
* @param splitter 分成多少段或是多少个线程下载
*/
public downloadinfo(string url, int splitter) {
this(url, null, null, splitter);
}
/***
* @param url 下载地址
* @param filename 文件名称
* @param filepath 文件保存路径
* @param splitter 分成多少段或是多少个线程下载
*/
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;
}
/**
* <b>function:</b> 通过url获得文件名称
* @author hoojo
* @createdate 2011-9-30 下午05:00:00
* @param url
* @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;
}
}
二、随机写入一段文件
复制代码 代码如下:
package com.hoo.download;
import java.io.ioexception;
import java.io.randomaccessfile;
/**
* <b>function:</b> 写入文件、保存文件
* @author hoojo
* @createdate 2011-9-21 下午05:44:02
* @file saveitemfile.java
* @package com.hoo.download
* @project multithreaddownload
* @blog http://blog.csdn.net/ibm_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
public class saveitemfile {
//存储文件
private randomaccessfile itemfile;
public saveitemfile() throws ioexception {
this("", 0);
}
/**
* @param name 文件路径、名称
* @param pos 写入点位置 position
* @throws ioexception
*/
public saveitemfile(string name, long pos) throws ioexception {
itemfile = new randomaccessfile(name, "rw");
//在指定的pos位置开始写入数据
itemfile.seek(pos);
}
/**
* <b>function:</b> 同步方法写入文件
* @author hoojo
* @createdate 2011-9-26 下午12:21:22
* @param buff 缓冲数组
* @param start 起始位置
* @param length 长度
* @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();
}
}
}
这个类主要是完成向本地的指定文件指针出开始写入文件,并返回当前写入文件的长度(文件指针)。这个类将被线程调用,文件被分成对应的块后,将被线程调用。每个线程都将会调用这个类完成文件的随机写入。
三、单个线程下载文件
复制代码 代码如下:
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;
/**
* <b>function:</b> 单线程下载文件
* @author hoojo
* @createdate 2011-9-22 下午02:55:10
* @file downloadfile.java
* @package com.hoo.download
* @project multithreaddownload
* @blog http://blog.csdn.net/ibm_hoojo
* @email hoojo_@126.com
* @version 1.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;
/**
* @param url 下载文件url
* @param name 文件名称
* @param startpos 下载文件起点
* @param endpos 下载文件结束点
* @param threadid 线程id
* @throws ioexception
*/
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;
}
}
/**
* <b>function:</b> 打印下载文件头部信息
* @author hoojo
* @createdate 2011-9-22 下午05:44:35
* @param conn 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;
}
}
}
/**
* <b>function:</b> 设置urlconnection的头部信息,伪装请求信息
* @author hoojo
* @createdate 2011-9-28 下午05:29:43
* @param con
*/
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;
}
}
这个类主要是完成单个线程的文件下载,将通过urlconnection读取指定url的资源信息。然后用inputstream读取文件内容,然后调用调用saveitemfile类,向本地写入当前要读取的块的内容。
四、分段多线程写入文件内容
复制代码 代码如下:
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;
/**
* <b>function:</b> 分批量下载文件
* @author hoojo
* @createdate 2011-9-22 下午05:51:54
* @file batchdownloadfile.java
* @package com.hoo.download
* @project multithreaddownload
* @blog http://blog.csdn.net/ibm_hoojo
* @email hoojo_@126.com
* @version 1.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!");
}
}
/**
* 将写入点数据保存在临时文件中
* @author hoojo
* @createdate 2011-9-23 下午05:25:37
* @throws ioexception
*/
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();
}
/**
* <b>function:</b>读取写入点的位置信息
* @author hoojo
* @createdate 2011-9-23 下午05:30:29
* @throws ioexception
*/
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();
}
/**
* <b>function:</b> 获取下载文件的长度
* @author hoojo
* @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;
}
}
这个类主要是完成读取指定url资源的内容,获取该资源的长度。然后将该资源分成指定的块数,将每块的起始下载位置、结束下载位置,分别保存在一个数组中。每块都单独开辟一个独立线程开始下载。在开始下载之前,需要创建一个临时文件,写入当前下载线程的开始下载指针位置和结束下载指针位置。
五、工具类、测试类
日志工具类
复制代码 代码如下:
package com.hoo.util;
/**
* <b>function:</b> 日志工具类
* @author hoojo
* @createdate 2011-9-21 下午05:21:27
* @file logutils.java
* @package com.hoo.util
* @project multithreaddownload
* @blog http://blog.csdn.net/ibm_hoojo
* @email hoojo_@126.com
* @version 1.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);
}
}
下载工具类
复制代码 代码如下:
package com.hoo.util;
import com.hoo.download.batchdownloadfile;
import com.hoo.entity.downloadinfo;
/**
* <b>function:</b> 分块多线程下载工具类
* @author hoojo
* @createdate 2011-9-28 下午05:22:18
* @file downloadutils.java
* @package com.hoo.util
* @project multithreaddownload
* @blog http://blog.csdn.net/ibm_hoojo
* @email hoojo_@126.com
* @version 1.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();
}
}
下载测试类
复制代码 代码如下:
package com.hoo.test;
import com.hoo.util.downloadutils;
/**
* <b>function:</b> 下载测试
* @author hoojo
* @createdate 2011-9-23 下午05:49:46
* @file testdownloadmain.java
* @package com.hoo.download
* @project multithreaddownload
* @blog http://blog.csdn.net/ibm_hoojo
* @email hoojo_@126.com
* @version 1.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);
}
}
多线程下载主要在第三部和第四部,其他的地方还是很好理解。源码中提供相应的注释了,便于理解。
上一篇: php通过GD库实现验证码功能
推荐阅读
-
使用java实现http多线程断点下载文件(二)
-
使用java实现http多线程断点下载文件(一)
-
Ruby中使用多线程队列(Queue)实现下载博客文章保存到本地文件
-
iOS开发-使用NSURLSession实现文件断点下载,文件离线续传以及图片上传
-
Java实现多线程断点下载
-
【Java多线程】使用多线程计算阶乘累加 1!+2!+3!+...+19!+20!。其中一个线程计算阶乘,另一线程实现累加并输出结果
-
从零开始,在java中使用七牛云实现文件云存储(一)
-
java使用FTP实现文件上传/下载
-
Java使用Sftp和Ftp实现对文件的上传和下载
-
夯实Java基础系列17:一文搞懂Java多线程使用方式、实现原理以及常见面试题