java 안드로이드에 적용되는 파일 다운로드 라인 클래스 구현

코드는 매우 간단하고 실용적이어서 여기는 쓸데없는 말이 많지 않으니, 직접 원본 코드를 보내시오

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;
  }
 
}
위에서 말한 것이 바로 본문의 전체 내용입니다. 여러분이 좋아하시기 바랍니다.

좋은 웹페이지 즐겨찾기