java實現(xiàn)適用于安卓的文件下載線程類
更新時間:2015年07月24日 11:39:00 投稿:hebedich
本文給大家分享的是java實現(xiàn)適用于安卓的文件下載線程類的代碼,有需要的小伙伴可以參考下
代碼非常簡單實用,這里就不多廢話了,直接奉上源碼
package android.mooc.tools;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.RandomAccessFile;
import java.net.URL;
import java.net.URLConnection;
import android.util.Log;
public class FileDownloadThread extends Thread {
private static final int BUFFER_SIZE = 1024;
private URL url;
private File file;
private int startPosition;
private int endPosition;
private int curPosition;
// 用于標(biāo)識當(dāng)前線程是否下載完成
private boolean finished = false;
private int downloadSize;
private boolean state;
boolean destory;
public boolean isDestory() {
return destory;
}
public void setDestory(boolean destory) {
this.destory = destory;
}
public FileDownloadThread(URL url, File file, int startPosition, int endPosition) {
this.url = url;
this.file = file;
this.startPosition = startPosition;
this.curPosition = startPosition;
this.endPosition = endPosition;
this.downloadSize = 0;
}
@Override
public void run() {
destory = false;
state = true;
BufferedInputStream bis = null;
RandomAccessFile fos = null;
byte[] buf = new byte[BUFFER_SIZE];
URLConnection con = null;
try {
con = url.openConnection();
con.setAllowUserInteraction(true);
// 設(shè)置當(dāng)前線程下載的起點,終點
con.setRequestProperty("Range", "bytes=" + startPosition + "-" + endPosition);
con.setRequestProperty("accept", "*/*");
con.setRequestProperty("connection", "Keep-Alive");
con.setRequestProperty("Accept-Language", "zh-CN");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322;"
+ " .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
// 使用java中的RandomAccessFile 對文件進(jìn)行隨機讀寫操作
fos = new RandomAccessFile(file, "rw");
// 設(shè)置開始寫文件的位置
fos.seek(startPosition);
bis = new BufferedInputStream(con.getInputStream());
// 開始循環(huán)以流的形式讀寫文件
while ((curPosition < endPosition) && (!destory)) {
while (state == false) {
sleep(2000);
}
int len = bis.read(buf, 0, BUFFER_SIZE);
if (len != -1) {
fos.write(buf, 0, len);
curPosition = curPosition + len;
if (curPosition > endPosition) {
downloadSize += len - (curPosition - endPosition);
} else {
downloadSize += len;
}
}
Log.i("333", "run" + " len=" + len);
}
// 下載完成設(shè)為true
this.finished = true;
bis.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
public boolean isFinished() {
return finished;
}
public int getDownloadSize() {
return downloadSize;
}
public void setDownloadSize(int downloadSize) {
this.downloadSize = downloadSize;
}
}
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
您可能感興趣的文章:
- 深入解析Java中ThreadLocal線程類的作用和用法
- java線程并發(fā)blockingqueue類使用示例
- java多線程并發(fā)executorservice(任務(wù)調(diào)度)類
- java線程之用Thread類創(chuàng)建線程的方法
- 詳談Java幾種線程池類型介紹及使用方法
- java多線程編程之使用Synchronized關(guān)鍵字同步類方法
- java多線程并發(fā)中使用Lockers類將多線程共享資源鎖定
- java線程并發(fā)countdownlatch類使用示例
- java多線程Future和Callable類示例分享
- java 可重啟線程及線程池類的設(shè)計(詳解)
- Java多線程繼承Thread類詳解
- Java繼承Thread類創(chuàng)建線程類示例
相關(guān)文章
Java?Scanner?類讀取一維數(shù)組二維數(shù)組示例詳解
這篇文章主要為大家介紹了Java?Scanner?類讀取一維數(shù)組二維數(shù)組示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
springboot集成Mybatis-plus-join-boot-start詳解
這篇文章主要介紹了springboot集成Mybatis-plus-join-boot-start方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
java從mysql導(dǎo)出數(shù)據(jù)的具體實例
這篇文章主要介紹了java從mysql導(dǎo)出數(shù)據(jù)的具體實例,有需要的朋友可以參考一下2013-12-12
五分鐘解鎖springboot admin監(jiān)控新技巧
本文不會講如何搭建企業(yè)的運維監(jiān)控系統(tǒng),有興趣的可以去找找成熟的比如Zabbix、Prometheus,甚至比較簡單的Wgcloud都能滿足一定的需求,不在此贅述。本文講解如何使用Springboot admin對spring boot項目進(jìn)行應(yīng)用監(jiān)控,感興趣的朋友一起看看吧2021-06-06
Java FileDescriptor總結(jié)_動力節(jié)點Java學(xué)院整理
FileDescriptor 是“文件描述符”??梢员挥脕肀硎鹃_放文件、開放套接字等。接下來通過本文給大家分享Java FileDescriptor總結(jié),感興趣的朋友一起學(xué)習(xí)吧2017-05-05

