当前位置: 移动技术网 > IT编程>开发语言>Java > java实现适用于安卓的文件下载线程类

java实现适用于安卓的文件下载线程类

2019年07月22日  | 移动技术网IT编程  | 我要评论
代码非常简单实用,这里就不多废话了,直接奉上源码 package android.mooc.tools; import java.io.bufferedi

代码非常简单实用,这里就不多废话了,直接奉上源码

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

以上所述就是本文的全部内容了,希望大家能够喜欢。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网