java 安卓下载文件_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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值