欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)適用于安卓的文件下載線程類

 更新時(shí)間:2015年07月24日 11:39:00   投稿:hebedich  
本文給大家分享的是java實(shí)現(xiàn)適用于安卓的文件下載線程類的代碼,有需要的小伙伴可以參考下

代碼非常簡(jiǎn)單實(shí)用,這里就不多廢話了,直接奉上源碼

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)識(shí)當(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)前線程下載的起點(diǎn),終點(diǎn)
      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 對(duì)文件進(jìn)行隨機(jī)讀寫操作
      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;
  }
 
}

以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。

相關(guān)文章

  • JAVA中使用雙括號(hào)來(lái)初始化靜態(tài)常量的小技巧

    JAVA中使用雙括號(hào)來(lái)初始化靜態(tài)常量的小技巧

    這篇文章主要介紹了JAVA中使用雙括號(hào)來(lái)初始化靜態(tài)常量的小技巧,需要的朋友可以參考下
    2014-06-06
  • 詳解OpenCV For Java環(huán)境搭建與功能演示

    詳解OpenCV For Java環(huán)境搭建與功能演示

    這篇文章主要介紹了x詳解OpenCV For Java環(huán)境搭建與功能演示,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • JAVA函數(shù)的定義、使用方法實(shí)例分析

    JAVA函數(shù)的定義、使用方法實(shí)例分析

    這篇文章主要介紹了JAVA函數(shù)的定義、使用方法,結(jié)合實(shí)例形式分析了JAVA函數(shù)的基本概念、功能、原理、定義、使用方法與操作注意事項(xiàng),需要的朋友可以參考下
    2020-04-04
  • Spring?Batch實(shí)現(xiàn)批量處理

    Spring?Batch實(shí)現(xiàn)批量處理

    本文主要介紹了Spring?Batch進(jìn)行批量處理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • 詳解Java利用同步塊synchronized()保證并發(fā)安全

    詳解Java利用同步塊synchronized()保證并發(fā)安全

    這篇文章主要介紹了Java利用同步塊synchronized()保證并發(fā)安全,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Spring AOP訪問(wèn)目標(biāo)方法的參數(shù)操作示例

    Spring AOP訪問(wèn)目標(biāo)方法的參數(shù)操作示例

    這篇文章主要介紹了Spring AOP訪問(wèn)目標(biāo)方法的參數(shù)操作,結(jié)合實(shí)例形式詳細(xì)分析了spring面向切面AOP訪問(wèn)目標(biāo)方法的參數(shù)相關(guān)實(shí)現(xiàn)步驟與操作注意事項(xiàng),需要的朋友可以參考下
    2020-01-01
  • 解決spring boot啟動(dòng)掃描不到自定義注解的問(wèn)題

    解決spring boot啟動(dòng)掃描不到自定義注解的問(wèn)題

    這篇文章主要介紹了解決spring boot啟動(dòng)掃描不到自定義注解的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-09-09
  • 基于Redis分布式鎖Redisson及SpringBoot集成Redisson

    基于Redis分布式鎖Redisson及SpringBoot集成Redisson

    這篇文章主要介紹了基于Redis分布式鎖Redisson及SpringBoot集成Redisson,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小小伙伴可以參考一下
    2022-09-09
  • springboot實(shí)現(xiàn)全局異常捕獲的使用示例

    springboot實(shí)現(xiàn)全局異常捕獲的使用示例

    任何系統(tǒng),我們不會(huì)傻傻的在每一個(gè)地方進(jìn)行異常捕獲和處理,整個(gè)系統(tǒng)一般我們會(huì)在一個(gè)的地方統(tǒng)一進(jìn)行異常處理,本文主要介紹了springboot實(shí)現(xiàn)全局異常捕獲的使用示例,感興趣的可以了解一下
    2023-11-11
  • 處理@PathVariable注解允許參數(shù)為空、允許不傳參數(shù)的問(wèn)題

    處理@PathVariable注解允許參數(shù)為空、允許不傳參數(shù)的問(wèn)題

    這篇文章主要介紹了處理@PathVariable注解允許參數(shù)為空、允許不傳參數(shù)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02

最新評(píng)論