Android进阶:实现多线程下载文件

简介:
 

多线程下载大概思路就是通过Range 属性实现文件分段,然后用RandomAccessFile 来读写文件,最终合并为一个文件

 

首先看下效果图

 

 

创建工程 ThreadDemo

 

首先布局文件 threaddemo.xml

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="下载地址"  
  11.     />  
  12. <TextView  
  13.     android:id="@+id/downloadurl"  
  14.     android:layout_width="fill_parent"   
  15.     android:layout_height="wrap_content"   
  16.     android:lines="5"  
  17.     />  
  18. <TextView    
  19.     android:layout_width="fill_parent"   
  20.     android:layout_height="wrap_content"   
  21.     android:text="线程数"  
  22.     />  
  23. <EditText  
  24.     android:id="@+id/downloadnum"  
  25.     android:layout_width="fill_parent"   
  26.     android:layout_height="wrap_content"   
  27.     />  
  28. <ProgressBar  
  29.     android:id="@+id/downloadProgressBar"  
  30.     android:layout_width="fill_parent"   
  31.     style="?android:attr/progressBarStyleHorizontal"  
  32.     android:layout_height="wrap_content"   
  33.     />  
  34. <TextView  
  35.     android:id="@+id/downloadinfo"  
  36.     android:layout_width="fill_parent"   
  37.     android:layout_height="wrap_content"   
  38.     android:text="下载进度 0"  
  39.     />  
  40. <Button  
  41.     android:id="@+id/downloadbutton"  
  42.     android:layout_width="wrap_content"   
  43.     android:layout_height="wrap_content"   
  44.     android:text="开始下载"  
  45.     />  
  46. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="下载地址" /><TextViewandroid:id="@+id/downloadurl"android:layout_width="fill_parent" android:layout_height="wrap_content" android:lines="5"/><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="线程数" /><EditTextandroid:id="@+id/downloadnum"android:layout_width="fill_parent" android:layout_height="wrap_content" /><ProgressBarandroid:id="@+id/downloadProgressBar"android:layout_width="fill_parent" style="?android:attr/progressBarStyleHorizontal" android:layout_height="wrap_content" /><TextViewandroid:id="@+id/downloadinfo"android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="下载进度 0"/><Buttonandroid:id="@+id/downloadbutton"android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始下载"/></LinearLayout>

 

主界面 Acitivity

 

  1. public class ThreadDownloadDemo extends Activity {  
  2.    
  3.     private TextView downloadurl;  
  4.     private EditText downloadnum;  
  5.     private Button downloadbutton;  
  6.     private ProgressBar downloadProgressBar;  
  7.     private TextView downloadinfo;  
  8.     private int downloadedSize = 0;  
  9.     private int fileSize = 0;  
  10.       
  11.     private long downloadtime;  
  12.    
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.threaddemo);  
  17.    
  18.         downloadurl = (TextView) findViewById(R.id.downloadurl);  
  19.         downloadurl.setText("http://file16.top100.cn/201105110911/AA5CC27CBE34DEB50A194581D1300881/Special_323149/%E8%8D%B7%E5%A1%98%E6%9C%88%E8%89%B2.mp3");  
  20.         downloadnum = (EditText) findViewById(R.id.downloadnum);  
  21.         downloadinfo = (TextView) findViewById(R.id.downloadinfo);  
  22.         downloadbutton = (Button) findViewById(R.id.downloadbutton);  
  23.         downloadProgressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);  
  24.         downloadProgressBar.setVisibility(View.VISIBLE);  
  25.         downloadProgressBar.setMax(100);  
  26.         downloadProgressBar.setProgress(0);  
  27.         downloadbutton.setOnClickListener(new OnClickListener() {  
  28.             public void onClick(View v) {  
  29.                 download();  
  30.                 downloadtime = SystemClock.currentThreadTimeMillis();  
  31.             }  
  32.         });  
  33.     }  
  34.    
  35.     private void download() {  
  36.         // 获取SD卡目录   
  37.         String dowloadDir = Environment.getExternalStorageDirectory()  
  38.                 + "/threaddemodownload/";  
  39.         File file = new File(dowloadDir);  
  40.         //创建下载目录   
  41.         if (!file.exists()) {  
  42.             file.mkdirs();  
  43.         }  
  44.           
  45.         //读取下载线程数,如果为空,则单线程下载   
  46.         int downloadTN = Integer.valueOf("".equals(downloadnum.getText()  
  47.                 .toString()) ? "1" : downloadnum.getText().toString());  
  48.         String fileName = "hetang.mp3";  
  49.         //开始下载前把下载按钮设置为不可用   
  50.         downloadbutton.setClickable(false);  
  51.         //进度条设为0   
  52.         downloadProgressBar.setProgress(0);  
  53.         //启动文件下载线程   
  54.         new downloadTask("http://file16.top100.cn/201105110911/AA5CC27CBE34DEB50A194581D1300881/Special_323149/%E8%8D%B7%E5%A1%98%E6%9C%88%E8%89%B2.mp3", Integer  
  55.                 .valueOf(downloadTN), dowloadDir + fileName).start();  
  56.     }  
  57.    
  58.     Handler handler = new Handler() {  
  59.         @Override  
  60.         public void handleMessage(Message msg) {  
  61.             //当收到更新视图消息时,计算已完成下载百分比,同时更新进度条信息   
  62.             int progress = (Double.valueOf((downloadedSize * 1.0 / fileSize * 100))).intValue();  
  63.             if (progress == 100) {  
  64.                 downloadbutton.setClickable(true);  
  65.                 downloadinfo.setText("下载完成!");  
  66.                 Dialog mdialog = new AlertDialog.Builder(ThreadDownloadDemo.this)  
  67.                     .setTitle("提示信息")  
  68.                     .setMessage("下载完成,总用时为:"+(SystemClock.currentThreadTimeMillis()-downloadtime)+"毫秒")  
  69.                     .setNegativeButton("确定"new DialogInterface.OnClickListener(){  
  70.                         @Override  
  71.                         public void onClick(DialogInterface dialog, int which) {  
  72.                             dialog.dismiss();  
  73.                         }  
  74.                     })  
  75.                     .create();  
  76.                 mdialog.show();  
  77.             } else {  
  78.                 downloadinfo.setText("当前进度:" + progress + "%");  
  79.             }  
  80.             downloadProgressBar.setProgress(progress);  
  81.         }  
  82.    
  83.     };  
  84.    
  85.       
  86.     public class downloadTask extends Thread {  
  87.         private int blockSize, downloadSizeMore;  
  88.         private int threadNum = 5;  
  89.         String urlStr, threadNo, fileName;  
  90.    
  91.         public downloadTask(String urlStr, int threadNum, String fileName) {  
  92.             this.urlStr = urlStr;  
  93.             this.threadNum = threadNum;  
  94.             this.fileName = fileName;  
  95.         }  
  96.    
  97.         @Override  
  98.         public void run() {  
  99.             FileDownloadThread[] fds = new FileDownloadThread[threadNum];  
  100.             try {  
  101.                 URL url = new URL(urlStr);  
  102.                 URLConnection conn = url.openConnection();  
  103.                 //防止返回-1   
  104.                 InputStream in = conn.getInputStream();  
  105.                 //获取下载文件的总大小   
  106.                 fileSize = conn.getContentLength();  
  107.                 Log.i("bb""======================fileSize:"+fileSize);  
  108.                 //计算每个线程要下载的数据量   
  109.                 blockSize = fileSize / threadNum;  
  110.                 // 解决整除后百分比计算误差   
  111.                 downloadSizeMore = (fileSize % threadNum);  
  112.                 File file = new File(fileName);  
  113.                 for (int i = 0; i < threadNum; i++) {  
  114.                     Log.i("bb""======================i:"+i);  
  115.                     //启动线程,分别下载自己需要下载的部分   
  116.                     FileDownloadThread fdt = new FileDownloadThread(url, file, i * blockSize, (i + 1) * blockSize - 1);  
  117.                     fdt.setName("Thread" + i);  
  118.                     fdt.start();  
  119.                     fds[i] = fdt;  
  120.                 }  
  121.                 boolean finished = false;  
  122.                 while (!finished) {  
  123.                     // 先把整除的余数搞定   
  124.                     downloadedSize = downloadSizeMore;  
  125.                     finished = true;  
  126.                     for (int i = 0; i < fds.length; i++) {  
  127.                         downloadedSize += fds[i].getDownloadSize();  
  128.                         if (!fds[i].isFinished()) {  
  129.                             finished = false;  
  130.                         }  
  131.                     }  
  132.                     handler.sendEmptyMessage(0);  
  133.                     //线程暂停一秒   
  134.                     sleep(1000);  
  135.                 }  
  136.             } catch (Exception e) {  
  137.                 e.printStackTrace();  
  138.             }  
  139.    
  140.         }  
  141.     }  
  142. }  
public class ThreadDownloadDemo extends Activity { private TextView downloadurl;private EditText downloadnum;private Button downloadbutton;private ProgressBar downloadProgressBar;private TextView downloadinfo;private int downloadedSize = 0;private int fileSize = 0;private long downloadtime; @Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.threaddemo); downloadurl = (TextView) findViewById(R.id.downloadurl);downloadurl.setText("http://file16.top100.cn/201105110911/AA5CC27CBE34DEB50A194581D1300881/Special_323149/%E8%8D%B7%E5%A1%98%E6%9C%88%E8%89%B2.mp3");downloadnum = (EditText) findViewById(R.id.downloadnum);downloadinfo = (TextView) findViewById(R.id.downloadinfo);downloadbutton = (Button) findViewById(R.id.downloadbutton);downloadProgressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);downloadProgressBar.setVisibility(View.VISIBLE);downloadProgressBar.setMax(100);downloadProgressBar.setProgress(0);downloadbutton.setOnClickListener(new OnClickListener() {public void onClick(View v) {download();downloadtime = SystemClock.currentThreadTimeMillis();}});} private void download() {// 获取SD卡目录String dowloadDir = Environment.getExternalStorageDirectory()+ "/threaddemodownload/";File file = new File(dowloadDir);//创建下载目录if (!file.exists()) {file.mkdirs();}//读取下载线程数,如果为空,则单线程下载int downloadTN = Integer.valueOf("".equals(downloadnum.getText().toString()) ? "1" : downloadnum.getText().toString());String fileName = "hetang.mp3";//开始下载前把下载按钮设置为不可用downloadbutton.setClickable(false);//进度条设为0downloadProgressBar.setProgress(0);//启动文件下载线程new downloadTask("http://file16.top100.cn/201105110911/AA5CC27CBE34DEB50A194581D1300881/Special_323149/%E8%8D%B7%E5%A1%98%E6%9C%88%E8%89%B2.mp3", Integer.valueOf(downloadTN), dowloadDir + fileName).start();} Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {//当收到更新视图消息时,计算已完成下载百分比,同时更新进度条信息int progress = (Double.valueOf((downloadedSize * 1.0 / fileSize * 100))).intValue();if (progress == 100) {downloadbutton.setClickable(true);downloadinfo.setText("下载完成!");Dialog mdialog = new AlertDialog.Builder(ThreadDownloadDemo.this).setTitle("提示信息").setMessage("下载完成,总用时为:"+(SystemClock.currentThreadTimeMillis()-downloadtime)+"毫秒").setNegativeButton("确定", new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();}}).create();mdialog.show();} else {downloadinfo.setText("当前进度:" + progress + "%");}downloadProgressBar.setProgress(progress);} }; public class downloadTask extends Thread {private int blockSize, downloadSizeMore;private int threadNum = 5;String urlStr, threadNo, fileName; public downloadTask(String urlStr, int threadNum, String fileName) {this.urlStr = urlStr;this.threadNum = threadNum;this.fileName = fileName;} @Overridepublic void run() {FileDownloadThread[] fds = new FileDownloadThread[threadNum];try {URL url = new URL(urlStr);URLConnection conn = url.openConnection();//防止返回-1InputStream in = conn.getInputStream();//获取下载文件的总大小fileSize = conn.getContentLength();Log.i("bb", "======================fileSize:"+fileSize);//计算每个线程要下载的数据量blockSize = fileSize / threadNum;// 解决整除后百分比计算误差downloadSizeMore = (fileSize % threadNum);File file = new File(fileName);for (int i = 0; i < threadNum; i++) {Log.i("bb", "======================i:"+i);//启动线程,分别下载自己需要下载的部分FileDownloadThread fdt = new FileDownloadThread(url, file, i * blockSize, (i + 1) * blockSize - 1);fdt.setName("Thread" + i);fdt.start();fds[i] = fdt;}boolean finished = false;while (!finished) {// 先把整除的余数搞定downloadedSize = downloadSizeMore;finished = true;for (int i = 0; i < fds.length; i++) {downloadedSize += fds[i].getDownloadSize();if (!fds[i].isFinished()) {finished = false;}}handler.sendEmptyMessage(0);//线程暂停一秒sleep(1000);}} catch (Exception e) {e.printStackTrace();} }}}

 

这里启动线程将文件分割为几个部分,每一个部分再启动一个线程去下载数据

 

下载文件的线程

 

  1. public class FileDownloadThread extends Thread{  
  2.     private static final int BUFFER_SIZE=1024;  
  3.     private URL url;  
  4.     private File file;  
  5.     private int startPosition;  
  6.     private int endPosition;  
  7.     private int curPosition;  
  8.     //标识当前线程是否下载完成   
  9.     private boolean finished=false;  
  10.     private int downloadSize=0;  
  11.     public FileDownloadThread(URL url,File file,int startPosition,int endPosition){  
  12.         this.url=url;  
  13.         this.file=file;  
  14.         this.startPosition=startPosition;  
  15.         this.curPosition=startPosition;  
  16.         this.endPosition=endPosition;  
  17.     }  
  18.     @Override  
  19.     public void run() {  
  20.         BufferedInputStream bis = null;  
  21.         RandomAccessFile fos = null;                                                 
  22.         byte[] buf = new byte[BUFFER_SIZE];  
  23.         URLConnection con = null;  
  24.         try {  
  25.             con = url.openConnection();  
  26.             con.setAllowUserInteraction(true);  
  27.             //设置当前线程下载的起止点   
  28.             con.setRequestProperty("Range""bytes=" + startPosition + "-" + endPosition);  
  29.             Log.i("bb", Thread.currentThread().getName()+"  bytes=" + startPosition + "-" + endPosition);  
  30.             //使用java中的RandomAccessFile 对文件进行随机读写操作   
  31.             fos = new RandomAccessFile(file, "rw");  
  32.             //设置写文件的起始位置   
  33.             fos.seek(startPosition);  
  34.             bis = new BufferedInputStream(con.getInputStream());    
  35.             //开始循环以流的形式读写文件   
  36.             while (curPosition < endPosition) {  
  37.                 int len = bis.read(buf, 0, BUFFER_SIZE);                  
  38.                 if (len == -1) {  
  39.                     break;  
  40.                 }  
  41.                 fos.write(buf, 0, len);  
  42.                 curPosition = curPosition + len;  
  43.                 if (curPosition > endPosition) {  
  44.                     downloadSize+=len - (curPosition - endPosition) + 1;  
  45.                 } else {  
  46.                     downloadSize+=len;  
  47.                 }  
  48.             }  
  49.             //下载完成设为true   
  50.             this.finished = true;  
  51.             bis.close();  
  52.             fos.close();  
  53.         } catch (IOException e) {  
  54.             e.printStackTrace();  
  55.         }  
  56.     }  
  57.    
  58.     public boolean isFinished(){  
  59.         return finished;  
  60.     }  
  61.    
  62.     public int getDownloadSize() {  
  63.         return downloadSize;  
  64.     }  
  65. }  
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=0;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;}@Overridepublic void run() { 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); Log.i("bb", Thread.currentThread().getName()+" bytes=" + startPosition + "-" + endPosition); //使用java中的RandomAccessFile 对文件进行随机读写操作 fos = new RandomAccessFile(file, "rw"); //设置写文件的起始位置 fos.seek(startPosition); bis = new BufferedInputStream(con.getInputStream()); //开始循环以流的形式读写文件 while (curPosition < endPosition) { int len = bis.read(buf, 0, BUFFER_SIZE); if (len == -1) { break; } fos.write(buf, 0, len); curPosition = curPosition + len; if (curPosition > endPosition) { downloadSize+=len - (curPosition - endPosition) + 1; } else { downloadSize+=len; } } //下载完成设为true this.finished = true; bis.close(); fos.close(); } catch (IOException e) { e.printStackTrace(); }} public boolean isFinished(){return finished;} public int getDownloadSize() {return downloadSize;}}

 

这里通过RandomAccessFile 的seek方法定位到相应的位置 并实时记录下载量

 

当然这里需要联网和访问SD卡 所以要加上相应的权限

 

<uses-permission android:name="android.permission.INTERNET" />  
  1.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>  
<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

 

这样就OK了 下面可以看看断点续传的问题了。有待测试~~

相关文章
|
1月前
|
Java 调度 Android开发
构建高效Android应用:探究Kotlin多线程编程
【2月更文挑战第17天】 在现代移动开发领域,性能优化一直是开发者关注的焦点。特别是在Android平台上,合理利用多线程技术可以显著提升应用程序的响应性和用户体验。本文将深入探讨使用Kotlin进行Android多线程编程的策略与实践,旨在为开发者提供系统化的解决方案和性能提升技巧。我们将从基础概念入手,逐步介绍高级特性,并通过实际案例分析如何有效利用Kotlin协程、线程池以及异步任务处理机制来构建一个更加高效的Android应用。
35 4
|
1月前
|
API 数据库 Android开发
构建高效Android应用:探究Kotlin多线程优化策略
【2月更文挑战第14天】随着移动设备性能的日益强大,用户对应用程序的响应速度和流畅性要求越来越高。在Android开发中,合理利用多线程技术是提升应用性能的关键手段之一。Kotlin作为一种现代的编程语言,其协程特性为开发者提供了更为简洁高效的多线程处理方式。本文将深入探讨使用Kotlin进行Android多线程编程的最佳实践,包括协程的基本概念、优势以及在实际项目中的应用场景和性能优化技巧,旨在帮助开发者构建更加高效稳定的Android应用。
|
2月前
|
Android开发
安卓SO层开发 -- 编译指定平台的SO文件
安卓SO层开发 -- 编译指定平台的SO文件
30 0
|
6天前
|
Java API 调度
安卓多线程和并发处理:提高应用效率
【4月更文挑战第13天】本文探讨了安卓应用中多线程和并发处理的优化方法,包括使用Thread、AsyncTask、Loader、IntentService、JobScheduler、WorkManager以及线程池。此外,还介绍了RxJava和Kotlin协程作为异步编程工具。理解并恰当运用这些技术能提升应用效率,避免UI卡顿,确保良好用户体验。随着安卓技术发展,更高级的异步处理工具将助力开发者构建高性能应用。
|
17天前
|
安全 Linux API
Android进程与线程
Android进程与线程
18 0
|
1月前
|
消息中间件 测试技术 Python
Python使用多线程解析超大日志文件
Python使用多线程解析超大日志文件
20 0
|
1月前
|
Shell 开发工具 Android开发
ADB 下载、安装及使用教程:让你更好地管理 Android 设备
ADB 下载、安装及使用教程:让你更好地管理 Android 设备
458 2
|
1月前
|
Java Android开发 开发者
构建高效Android应用:探究Kotlin多线程优化策略
【2月更文挑战第17天】 随着移动设备性能的不断提升,用户对应用的响应速度和稳定性要求越来越高。在Android开发中,Kotlin语言以其简洁、安全的特点受到开发者青睐。然而,面对复杂的多线程任务,如何有效利用Kotlin进行优化,以提升应用性能,是本文探讨的重点。通过分析Kotlin并发工具的使用场景与限制,结合实例演示其在Android开发中的实践,旨在为开发者提供实用的多线程处理指南。
|
1月前
|
Android开发 对象存储
OSS对象储存android开发进行下载到本地文件时异步操作失效
android vivo80使用官方示例代码进行文件下载,但是使用oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>()时onSuccess和onFailure不执行
|
2月前
|
JSON Java Go