java實現適用於安卓的檔案下載線程類_java

來源:互聯網
上載者:User

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

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;  } }

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.