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

 更新时间:2015年07月24日 11:39:00   投稿:hebedich  
本文给大家分享的是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;
  }
 
}

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

相关文章

  • Java Scanner 类读取一维数组二维数组示例详解

    Java Scanner 类读取一维数组二维数组示例详解

    这篇文章主要为大家介绍了Java Scanner 类读取一维数组二维数组示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-11-11
  • Java 实现对称加密算法

    Java 实现对称加密算法

    这篇文章主要介绍了Java 实现对称加密算法的相关资料,帮助大家更好的理解和学习Java,感兴趣的朋友可以了解下
    2020-11-11
  • SpringMVC中处理Ajax请求的示例

    SpringMVC中处理Ajax请求的示例

    本篇文章给大家介绍SpringMVC中处理Ajax请求的示例,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2023-11-11
  • springboot集成Mybatis-plus-join-boot-start详解

    springboot集成Mybatis-plus-join-boot-start详解

    这篇文章主要介绍了springboot集成Mybatis-plus-join-boot-start方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-04-04
  • java从mysql导出数据的具体实例

    java从mysql导出数据的具体实例

    这篇文章主要介绍了java从mysql导出数据的具体实例,有需要的朋友可以参考一下
    2013-12-12
  • Java无限级树(递归)超实用案例

    Java无限级树(递归)超实用案例

    下面小编就为大家带来一篇Java无限级树(递归)超实用案例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-11-11
  • 重新认识Java中的ThreadLocal

    重新认识Java中的ThreadLocal

    ThreadLocal是JDK包提供的,它提供线程本地变量,如果创建一个ThreadLocal变量,那么访问这个变量的每个线程都会有这个变量的一个副本,在实际多线程操作的时候,操作的是自己本地内存中的变量,从而规避了线程安全问题
    2021-05-05
  • 五分钟解锁springboot admin监控新技巧

    五分钟解锁springboot admin监控新技巧

    本文不会讲如何搭建企业的运维监控系统,有兴趣的可以去找找成熟的比如Zabbix、Prometheus,甚至比较简单的Wgcloud都能满足一定的需求,不在此赘述。本文讲解如何使用Springboot admin对spring boot项目进行应用监控,感兴趣的朋友一起看看吧
    2021-06-06
  • SpringBoot返回所有接口详细信息的方法详解

    SpringBoot返回所有接口详细信息的方法详解

    这篇文章主要介绍了SpringBoot返回所有接口详细信息的方法,简单来说就是我们通过访问一个接口能看到我们所有的API接口的数量,以及路径和请求方法,文中有详细的代码供大家参考,需要的朋友可以参考下
    2025-04-04
  • Java FileDescriptor总结_动力节点Java学院整理

    Java FileDescriptor总结_动力节点Java学院整理

    FileDescriptor 是“文件描述符”。可以被用来表示开放文件、开放套接字等。接下来通过本文给大家分享Java FileDescriptor总结,感兴趣的朋友一起学习吧
    2017-05-05

最新评论