命令行使用支持断点续传的java多线程下载器
package org.load.download;
import java.io.file;
import java.io.ioexception;
import java.io.inputstream;
import java.io.randomaccessfile;
import java.text.decimalformat;
import org.apache.http.httpentity;
import org.apache.http.httpresponse;
import org.apache.http.client.clientprotocolexception;
import org.apache.http.client.httpclient;
import org.apache.http.client.methods.httpget;
import org.apache.http.impl.client.defaulthttpclient;
public class download {
public static void main(string[] args) {
new thread(new d("http://al.jb51.net:81/200812/tools/ha_leapftp.rar"))
.start();
new thread(
new d(
"http://al.jb51.net:81/200812/tools/ha_leapftp.rar"))
.start();
}
}
class d implements runnable {
private static final string path = "e:\\download";
private string url;
private string filename = null;
static {
if (!new file(path).exists()) {
new file(path).mkdirs();
}
}
public d(string url) {
this.url = url;
this.filename = this.url.substring(this.url.lastindexof('/') + 1,
this.url.length()); // 得到文件名
}
public void download() throws clientprotocolexception, ioexception {
final randomaccessfile file = new randomaccessfile(this.path + file.separator
+ this.filename, "rw");
httpclient client = new defaulthttpclient();
httpget get = new httpget(this.url);
// client.getparams().setparameter("http.socket.timeout", 5000); // 设置连接超时
long localfilesize = this.getlocalfilesize();
final long remotefilesize = this.getremotefilesize();
// 如果本地文件未下载完成,则断点下载
if (-1 != localfilesize && -1 != remotefilesize
&& localfilesize < remotefilesize) {
file.seek(localfilesize); // 本地标记
get.addheader("range", "bytes=" + localfilesize + "-"
+ remotefilesize); // 远程标记
}
// 如果本地文件大小大于等于远程文件,则已经下载完成
if (-1 != localfilesize && localfilesize >= remotefilesize) {
return;
}
// 开始下载
httpresponse response = client.execute(get);
if (300 >= response.getstatusline().getstatuscode()) {
httpentity en = response.getentity();
inputstream in = en.getcontent();
byte[] by = new byte[512];
int len = -1;
// 显示进度
new thread(new runnable(){
@override
public void run() {
try {
while (file.length() < remotefilesize) {
// runtime.getruntime().exec("cmd cls"); // 听说会另起个进程
system.out.println(filename
+ "已下载:\t"
+ new decimalformat("0.00%").format(file
.length() / (double) remotefilesize));
thread.sleep(5000);
}
} catch (ioexception e) {
e.printstacktrace();
} catch (interruptedexception e) {
e.printstacktrace();
}
}
}).start();
// 开始下载
while (-1 != (len = in.read(by))) {
file.write(by, 0, len);
}
in.close();
client.getconnectionmanager().shutdown();
}
}
// 得到本地文件大小
private long getlocalfilesize() {
file file = new file(path + file.separator + this.filename);
if (!file.exists()) {
return -1l;
}
return file.length();
}
// 得到远程文件大小
private long getremotefilesize() throws clientprotocolexception,
ioexception {
httpclient client = new defaulthttpclient();
httpget get = new httpget(this.url);
client.getparams().setparameter("http.socket.timeout", 5000);
httpresponse response = client.execute(get);
if (200 == response.getstatusline().getstatuscode()
|| 300 >= response.getstatusline().getstatuscode()) {
httpentity en = response.getentity();
return en.getcontentlength();
}
return -1l;
}
@override
public void run() {
try {
download();
system.out.println(this.filename + "\t下载完成");
} catch (clientprotocolexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
}
}
}
上一篇: Dubbo无法访问远程Zookeeper已注册服务的问题解决方案
下一篇: 详解JDBC使用
推荐阅读
-
命令行使用支持断点续传的java多线程下载器
-
命令行使用支持断点续传的java多线程下载器
-
Java swing 带界面和进度条的多线程下载器实现
-
python支持断点续传的多线程下载示例
-
python使用urllib模块开发的多线程豆瓣小站mp3下载器
-
python支持断点续传的多线程下载示例
-
使用文本编辑器+命令行的方式实现Java中的第一个程序Hello World(上)
-
Java 服务器端支持断点续传的源代码【支持快车、迅雷】(仅支持 HTTP 协议)
-
chrome谷歌浏览器:您使用的是不受支持的命令行标记:--extensions-on-chrome-urls
-
【Java用法】使用Java导出word文档的解决方案(适用于从服务器上下载到本地电脑)