Android 多线程下载


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

保存为test.mp3文件


相关内容