命令行使用支持?jǐn)帱c(diǎn)續(xù)傳的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); // 設(shè)置連接超時(shí)
long localFileSize = this.getLocalFileSize();
final long remoteFileSize = this.getRemoteFileSize();
// 如果本地文件未下載完成,則斷點(diǎn)下載
if (-1 != localFileSize && -1 != remoteFileSize
&& localFileSize < remoteFileSize) {
file.seek(localFileSize); // 本地標(biāo)記
get.addHeader("Range", "bytes=" + localFileSize + "-"
+ remoteFileSize); // 遠(yuǎn)程標(biāo)記
}
// 如果本地文件大小大于等于遠(yuǎn)程文件,則已經(jīng)下載完成
if (-1 != localFileSize && localFileSize >= remoteFileSize) {
return;
}
// 開(kāi)始下載
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;
// 顯示進(jìn)度
new Thread(new Runnable(){
@Override
public void run() {
try {
while (file.length() < remoteFileSize) {
// Runtime.getRuntime().exec("cmd cls"); // 聽(tīng)說(shuō)會(huì)另起個(gè)進(jìn)程
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();
// 開(kāi)始下載
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();
}
// 得到遠(yuǎn)程文件大小
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();
}
}
}
- 很簡(jiǎn)單的Java斷點(diǎn)續(xù)傳實(shí)現(xiàn)原理
- Java如何實(shí)現(xiàn)HTTP斷點(diǎn)續(xù)傳功能
- java實(shí)現(xiàn)文件斷點(diǎn)續(xù)傳下載功能
- RxJava+Retrofit+OkHttp實(shí)現(xiàn)多文件下載之?dāng)帱c(diǎn)續(xù)傳
- java斷點(diǎn)續(xù)傳功能實(shí)例(java獲取遠(yuǎn)程文件)
- Java實(shí)現(xiàn)的斷點(diǎn)續(xù)傳功能的示例代碼
- Java編程實(shí)現(xiàn)服務(wù)器端支持?jǐn)帱c(diǎn)續(xù)傳的方法(可支持快車、迅雷)
- 基于Ok+Rxjava+retrofit實(shí)現(xiàn)斷點(diǎn)續(xù)傳下載
- 基于Java實(shí)現(xiàn)多線程下載并允許斷點(diǎn)續(xù)傳
- java實(shí)現(xiàn)文件的斷點(diǎn)續(xù)傳
相關(guān)文章
java搭建ftp/sftp進(jìn)行數(shù)據(jù)傳遞的全過(guò)程
ftp是一種文件傳輸協(xié)議,讓客戶端和服務(wù)端能夠互相傳遞文件,圖片等數(shù)據(jù),sftp也是一種文件傳輸協(xié)議,但是相比較而言要比f(wàn)tp安全性更好些,但是也有缺點(diǎn)就是傳輸效率低2021-07-07
Java判斷2個(gè)List集合是否相等(不考慮元素的順序)
今天小編就為大家分享一篇關(guān)于Java判斷2個(gè)List集合是否相等(不考慮元素的順序)的文章,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10
MyBatis中常見(jiàn)的SQL執(zhí)行方式及其使用方法
MyBatis可能很多人都一直在用,但是MyBatis的SQL執(zhí)行流程可能并不是所有人都清楚了,下面這篇文章主要給大家介紹了關(guān)于MyBatis中常見(jiàn)的SQL執(zhí)行方式及其使用的相關(guān)資料,需要的朋友可以參考下2023-09-09
Spring?Boot?MQTT?Too?many?publishes?in?progress錯(cuò)誤的解決方
本文介紹Spring?Boot?MQTT?Too?many?publishes?in?progress錯(cuò)誤的解決方案,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-07-07
Java關(guān)鍵字synchronized原理與鎖的狀態(tài)詳解
在Java當(dāng)中synchronized關(guān)鍵字通常是用來(lái)標(biāo)記一個(gè)方法或者代碼塊。本文將通過(guò)示例為大家詳細(xì)介紹一下Synchronized的各種使用方法,需要的可以參考一下2022-08-08

