Android 使用Get方式实现断点下载(一)
发布日期:2021-06-30 22:35:08
浏览次数:4
分类:技术文章
本文共 3854 字,大约阅读时间需要 12 分钟。
public class DownloadActivity extends Activity implements OnClickListener{ private String path; private static final int THREAD_NUM = 3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = new Button(this); button.setOnClickListener(this); } @Override public void onClick(View v) { download(path); } private void download(String path) { try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); if (conn.getResponseCode() == 200) { int length = conn.getContentLength(); RandomAccessFile raf = new RandomAccessFile("down.exe", "rwd"); raf.setLength(length); raf.close(); int blocksize = length / THREAD_NUM; for (int i = 1; i <= THREAD_NUM; i++) { int start = (i-1) * blocksize; int end = i * blocksize - 1; if (i == THREAD_NUM) { end = length; } System.out.println("线程:"+i + "下载:" + start + "---->" + end); new DownloadThread(i, start, end, path).start(); } }else { System.out.println("服务器错误!"); } } catch (Exception e) { e.printStackTrace(); } } class DownloadThread extends Thread{ private int threadId; private int start; private int end; private String path; public DownloadThread(int threadId,int start,int end, String path){ this.threadId = threadId; this.start = start; this.end = end; this.path = path; } @Override public void run() { try { int tempLen=0; //先读取临时文件 File temFile = new File(threadId+".txt"); if (temFile.exists()) { FileInputStream fis = new FileInputStream(temFile); byte[] buffer = new byte[1024]; int len = fis.read(buffer); tempLen = Integer.parseInt(new String(buffer,0,len)); start += tempLen; } URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Range", "bytes=" + start + "-" + end); conn.setConnectTimeout(5000); int total=0; if (conn.getResponseCode() == 206) { InputStream inputStream = conn.getInputStream(); RandomAccessFile raf = new RandomAccessFile("down.exe", "rwd"); raf.seek(start); byte[] buffer = new byte[1024]; int len = -1; while((len = inputStream.read(buffer))!=-1){ RandomAccessFile tempRaf = new RandomAccessFile(threadId+".txt", "rwd"); raf.write(buffer, 0 , len); total+=len; System.out.println("线程:"+threadId + "下载total:" + total); tempRaf.write(((tempLen + total) + "").getBytes()); tempRaf.close(); } raf.close(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }}
转载地址:https://lovoo.blog.csdn.net/article/details/51232042 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年05月01日 21时16分03秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
gym 101137 L Lazy Coordinator(概率)
2019-04-30
Mysql 入门
2019-04-30
Vue 之 axios 篇
2019-04-30
vue 之动画
2019-04-30
js的基础语法
2019-04-30
vue使用iconfont
2019-04-30
linux 查看文件夹权限
2019-04-30
linux tar 备份
2019-04-30
ubuntu中配置任意应用的快捷键
2019-04-30
ubuntu 快捷键
2019-04-30
linux 根目录下文件夹分析
2019-04-30
My notes about backup to ubuntu
2019-04-30
linux 查看分区和文件大小
2019-04-30
Not using PCAP_FRAMES 解释(snort中)
2019-04-30
技术转管理?这些“坑”你要绕道走
2019-04-30
领域驱动设计(DDD)前夜:面向对象思想
2019-04-30
Ubuntu 14.04 安装TM2009/QQ
2019-04-30
Camera驱动调试小记
2019-04-30
linux嵌入式系统开发之触摸屏---驱动篇(上/硬件原理\下/源码分析)
2019-04-30