歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android中級篇之多線程下載

Android中級篇之多線程下載

日期:2017/3/1 10:50:21   编辑:Linux編程
要是先多線程下載,則必須對同一個文件可任意位置的寫入 ,java中提供這樣一個類可任意寫入RandomAccessFile 。通過多線程,可將文件分割成多個子斷,每一個線程只需下載一段文件即可。實現效果如圖:



下面看代碼部分:

1.布局文件 main.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. <EditText
  13. android:id="@+id/mEditText"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:singleLine="true"
  17. android:text="http://android.yesky.com/uploads/attachments/2010-04/27/a5964152.jpg"/>
  18. <ProgressBar
  19. android:id="@+id/mBar"
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:visibility="invisible"
  23. style="?android:attr/progressBarStyleHorizontal"/>
  24. <Button
  25. android:id="@+id/mButton"
  26. android:layout_width="fill_parent"
  27. android:layout_height="wrap_content"
  28. android:text="下載"/>
  29. </LinearLayout>


2.下載工具類 Download.java

  1. package com.yin.downloader.utils;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.RandomAccessFile;
  6. import java.net.HttpURLConnection;
  7. import java.net.MalformedURLException;
  8. import java.net.URL;
  9. import android.content.Context;
  10. import android.util.Log;
  11. public class Download {
  12. private String SDPath = null;
  13. private static final String TAG = "com.yin.download";
  14. private RandomAccessFile randomFile = null;
  15. private URL url;
  16. private Context context;
  17. private String urlStr;
  18. private String fileName;
  19. private int fileSize;
  20. private int totalReadSize;
  21. public Download(String urlStr,String fileName,String SDPath,Context context){
  22. this.context = context;
  23. this.fileName = fileName;
  24. this.urlStr = urlStr;
  25. this.SDPath = SDPath;
  26. }
  27. public void downloadFile(){
  28. try {
  29. File file = new File(SDPath+fileName);
  30. url = new URL(urlStr);
  31. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  32. conn.setRequestMethod("GET");
  33. //獲得下載文件的大小
  34. fileSize = conn.getContentLength();
  35. //設置線程的大小,考慮手機的性能,並不是越大越好
  36. int threadSize = 3;
  37. //每個線程下載文件+的部分大小 +1 避免文件下載不完全
  38. int block = fileSize / threadSize +1;
  39. for(int i=0;i<3;i++){
  40. int startPosition = i * block;
  41. //創建可任意位置讀取的文件
  42. randomFile = new RandomAccessFile(file, "rw");
  43. randomFile.seek(startPosition);
  44. new DownloadThread(i+1, startPosition, block, randomFile).start();
  45. }
  46. } catch (MalformedURLException e) {
  47. Log.e(TAG, "MalformedURLException");
  48. e.printStackTrace();
  49. } catch (IOException e) {
  50. Log.e(TAG, "IOException");
  51. e.printStackTrace();
  52. }
  53. }
  54. //下載文件的線程
  55. private class DownloadThread extends Thread{
  56. private int threadID;
  57. private int startPosition;
  58. private int block;
  59. private RandomAccessFile randomFile;
  60. public DownloadThread(int threadID, int startPosition, int block,
  61. RandomAccessFile randomFile) {
  62. super();
  63. this.threadID = threadID;
  64. this.startPosition = startPosition;
  65. this.block = block;
  66. this.randomFile = randomFile;
  67. }
  68. public void run() {
  69. try {
  70. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  71. conn.setRequestMethod("GET");
  72. //文件下載位置 規定的格式 “byte=xxxx-”
  73. String start = "bytes="+startPosition + "-";
  74. //設置文件開始的下載位置
  75. conn.setRequestProperty("Range", start);
  76. InputStream is = conn.getInputStream();
  77. byte[] buffer = new byte[4*1024];
  78. int len = -1;
  79. int readFileSize = 0;
  80. while((readFileSize < block) && ((len = is.read(buffer)) != -1)){
  81. randomFile.write(buffer,0,len);
  82. readFileSize += len ;
  83. totalReadSize += readFileSize;
  84. }
  85. System.out.println("線程 :"+threadID+" 下載完成");
  86. is.close();
  87. randomFile.close();
  88. conn.disconnect();
  89. } catch (IOException e) {
  90. Log.e(TAG+":child", "IOException");
  91. e.printStackTrace();
  92. }
  93. }
  94. }
  95. public int getFileSize() {
  96. return fileSize;
  97. }
  98. public void setFileSize(int fileSize) {
  99. this.fileSize = fileSize;
  100. }
  101. public int getTotalReadSize() {
  102. return totalReadSize;
  103. }
  104. public void setTotalReadSize(int totalReadSize) {
  105. this.totalReadSize = totalReadSize;
  106. }
  107. }
Copyright © Linux教程網 All Rights Reserved