时间:2021-05-19
代码非常简单实用,这里就不多废话了,直接奉上源码
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; } }以上所述就是本文的全部内容了,希望大家能够喜欢。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
ThreadLocalThreadLocal适用于变量在线程间隔离,而在方法或类间共享的场景。代码@RestControllerpublicclassThrea
本文实例讲述了java实现的正则工具类。分享给大家供大家参考。具体如下:这里实现的正则工具类适用于:正则电话号码、邮箱、QQ号码、QQ密码、手机号java代码如
”随着安卓手机平台的迅速崛起,适用于安卓手机的应用软件及游戏也越来越多。如版需下载第三方应用权软件或者好玩的游戏及应用,建议可以通过自带的应用商店或
主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java。目前包括HttpUtils、DownloadManagerPro、ShellUtils
手机淘宝是整合了天猫,聚划算,淘宝商城为一体的一款手机应用软件,目前适用于安卓、ios、java、塞班等移动平台。手机淘宝已经是不可被忽视的流量板块啦,在做手机