国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java實現適用于安卓的文件下載線程類

2019-11-26 15:04:13
字體:
來源:轉載
供稿:網友

代碼非常簡單實用,這里就不多廢話了,直接奉上源碼

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;  // 用于標識當前線程是否下載完成  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);      // 設置當前線程下載的起點,終點      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 對文件進行隨機讀寫操作      fos = new RandomAccessFile(file, "rw");      // 設置開始寫文件的位置      fos.seek(startPosition);      bis = new BufferedInputStream(con.getInputStream());      // 開始循環以流的形式讀寫文件      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);      }      // 下載完成設為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;  } }

以上所述就是本文的全部內容了,希望大家能夠喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临海市| 武宁县| 甘肃省| 逊克县| 浙江省| 河池市| 巴林右旗| 绥化市| 金平| 长岭县| 甘肃省| 彭阳县| 沭阳县| 连城县| 甘孜县| 洛川县| 托克逊县| 石屏县| 南充市| 望都县| 静宁县| 汨罗市| 静海县| 昌吉市| 金川县| 股票| 蒙自县| 丹江口市| 武宁县| 竹北市| 河池市| 麻江县| 景东| 济阳县| 徐州市| 曲松县| 嘉定区| 楚雄市| 拉萨市| 平远县| 沅江市|