Android断点续传学习

来源:互联网 发布:xl player for mac 编辑:程序博客网 时间:2024/06/14 10:26

先学习断点续传热热身,为多线程下载预热;

其实重点就是下载这里;


1,下载范围的设定

 urlConnection.setRequestProperty("Range","bytes=" + start + "-" + info.getLength());  


2 , 文件移动到指定位置

randomFile.seek(start); 


其他的没有难度,很简单;可以使用数据库或者文件作为缓存文件

主要是一个思路;这里简单的记录一下;作为学习记录



  1.    class MyThread extends Thread{  
  2.         private FileInfo info = null;  
  3.         public MyThread(FileInfo threadInfo) {  
  4.             this.info = threadInfo;  
  5.         }  
  6.         @Override  
  7.         public void run() {  
  8.             //向数据库添加线程信息  
  9.             if(!threadDAO.isExits(info.getUrl())){  
  10.                 threadDAO.insert(info);  
  11.             }  
  12.             HttpURLConnection urlConnection = null;  
  13.             RandomAccessFile randomFile =null;  
  14.             InputStream inputStream = null;  
  15.             try {  
  16.                 URL url = new URL(info.getUrl());  
  17.                 urlConnection = (HttpURLConnection) url.openConnection();  
  18.                 urlConnection.setConnectTimeout(3000);  
  19.                 urlConnection.setRequestMethod("GET");  
  20.                 //设置下载位置  
  21.                 int start = info.getStart() + info.getNow();  
  22.                 urlConnection.setRequestProperty("Range","bytes=" + start + "-" + info.getLength());  
  23.   
  24.                 //设置文件写入位置  
  25.                 File file = new File(DOWNLOAD_PATH,FILE_NAME);  
  26.                 randomFile = new RandomAccessFile(file, "rwd");  
  27.                 randomFile.seek(start);  
  28.   
  29.                 //向Activity发广播  
  30.                 Intent intent = new Intent(ACTION_UPDATE);  
  31.                 finished += info.getNow();  
  32.   
  33.                 if (urlConnection.getResponseCode() == HttpStatus.SC_PARTIAL_CONTENT) {  
  34.                     //获得文件流  
  35.                     inputStream = urlConnection.getInputStream();  
  36.                     byte[] buffer = new byte[512];  
  37.                     int len = -1;  
  38.                     long time = System.currentTimeMillis();  
  39.                     while ((len = inputStream.read(buffer))!= -1){  
  40.                         //写入文件  
  41.                         randomFile.write(buffer,0,len);  
  42.                         //把进度发送给Activity  
  43.                         finished += len;  
  44.                         //看时间间隔,时间间隔大于500ms再发  
  45.                         if(System.currentTimeMillis() - time >500){  
  46.                             time = System.currentTimeMillis();  
  47.                             intent.putExtra("now",finished *100 /fileInfo.getLength());  
  48.                             context.sendBroadcast(intent);  
  49.                         }  
  50.                         //判断是否是暂停状态  
  51.                         if(isPause){  
  52.                             threadDAO.update(info.getUrl(),finished);  
  53.                             return//结束循环  
  54.                         }  
  55.                     }  
  56.                     //删除线程信息  
  57.                     threadDAO.delete(info.getUrl());  
  58.                 }  
  59.             }catch (Exception e){  
  60.                 e.printStackTrace();  
  61.             }finally {//回收工作略  
  62.             }  
  63.         }  
  64.     }  
  65. }  

原创粉丝点击