歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 多線程下載

Android 多線程下載

日期:2017/3/1 11:07:31   编辑:Linux編程
  1. package my.Thread;
  2. import java.io.BufferedInputStream;
  3. import java.io.RandomAccessFile;
  4. import java.net.HttpURLConnection;
  5. import java.net.URL;
  6. import java.net.URLConnection;
  7. import java.util.concurrent.CountDownLatch;
  8. import Android.app.Activity;
  9. import android.os.Bundle;
  10. import android.os.Environment;
  11. import android.view.View;
  12. import android.view.View.OnClickListener;
  13. import android.widget.Button;
  14. import android.widget.TextView;
  15. public class ManyThreadActivity extends Activity {
  16. /** Called when the activity is first created. */
  17. private Button button;
  18. private TextView textView;
  19. private static final int THREAD_COUNT = 4;
  20. private CountDownLatch latch = new CountDownLatch(THREAD_COUNT);
  21. private long completeLength = 0;
  22. private long fileLength;
  23. public static String SaveFile=Environment.getExternalStorageDirectory()+"/DownFile/"+"test.Mp3";
  24. public static final String url="http://210.30.12.33:8080/mp3/Beyond.mp3";
  25. @Override
  26. public void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.main);
  29. button=(Button)findViewById(R.id.button1);
  30. textView=(TextView) findViewById(R.id.editText1);
  31. textView.setText(url);
  32. button.setOnClickListener(new MyListener());
  33. }
  34. class MyListener implements OnClickListener{
  35. @Override
  36. public void onClick(View v) {
  37. // TODO Auto-generated method stub
  38. try {
  39. download(url);
  40. } catch (Exception e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. }
  44. }
  45. }
  46. //進行下載的線程
  47. class DownloadThread extends Thread {
  48. private URL url;
  49. private RandomAccessFile file;
  50. private long from;
  51. private long end;
  52. /**
  53. * @param url 下載的URL
  54. * @param file 下載完成之後存儲的文件
  55. * @param from 當前線程對應文件的起始位置
  56. * @param end 當前線程對應文件的結束位置
  57. */
  58. DownloadThread(URL url, RandomAccessFile file, long from, long end) {
  59. this.url = url;
  60. this.file = file;
  61. this.from = from;
  62. this.end = end;
  63. }
  64. public void run() {
  65. try {
  66. long pos = from;
  67. byte[] buf = new byte[1024*8];
  68. HttpURLConnection cn = (HttpURLConnection) url.openConnection();
  69. //設置請求的起始和結束位置。
  70. cn.setRequestProperty("Range", "bytes=" + from + "-" + end);
  71. BufferedInputStream bis = new BufferedInputStream(cn.getInputStream());
  72. int len;
  73. while ((len = bis.read(buf)) > 0) {
  74. synchronized (file) {
  75. file.seek(pos);
  76. file.write(buf, 0, len);
  77. }
  78. pos += len;
  79. completeLength += len;
  80. System.out.println(completeLength * 100 / fileLength + "%");
  81. }
  82. cn.disconnect();
  83. } catch (Exception e) {
  84. e.printStackTrace();
  85. }
  86. latch.countDown();
  87. }
  88. }
  89. public void download(String address) throws Exception {
  90. URL url = new URL(address);
  91. URLConnection cn = url.openConnection();
  92. fileLength = cn.getContentLength();
  93. long packageLength = fileLength / THREAD_COUNT;//每個線程要下載的字節數
  94. long leftLength = fileLength % THREAD_COUNT;//剩下的字節數
  95. RandomAccessFile file = new RandomAccessFile(SaveFile, "rw");
  96. System.out.println(fileLength);
  97. //計算每個線程請求的起始位置和結束位置。
  98. /*
  99. * 第一個線程的起始位置是0~0+packageLength(每個線程要下載的字節數)
  100. * 第二個線程的起始位置是endPos+1(第一個線程的packageLength+1)~endPos+1+packageLength
  101. * 第二個線程的起始位置是.........
  102. */
  103. long pos = 0;
  104. for (int i = 0; i < THREAD_COUNT; i++) {
  105. long endPos = pos + packageLength;
  106. new DownloadThread(url, file, pos, endPos).start();
  107. if(leftLength > 0) {
  108. endPos++;
  109. leftLength--;
  110. }
  111. pos = endPos;
  112. }
  113. latch.await();
  114. }
  115. }

保存為test.mp3文件


Copyright © Linux教程網 All Rights Reserved