Android通过HTTP协议实现多线程下载

Android通过HTTP协议实现多线程下载


转:http://blog.csdn.net/jiabinjlu/article/details/21551105

  1. import java.io.File;  
  2. import java.io.InputStream;  
  3. import java.io.RandomAccessFile;  
  4. import java.net.HttpURLConnection;  
  5. import java.net.URL;  
  6.   
  7. public class MulThreadDownload {  
  8.   
  9.     /** 
  10.      * @param args 
  11.      */  
  12.     public static void main(String[] args) {  
  13.         String path = "http://net.itcast.cn/QQWubiSetup.exe";  
  14.         try {  
  15.             new MulThreadDownload().download(path, 3);  
  16.         } catch (Exception e) {  
  17.             e.printStackTrace();  
  18.         }  
  19.     }  
  20.     /** 
  21.      * 从路径中获取文件名称 
  22.      * @param path 下载路径 
  23.      * @return 
  24.      */  
  25.     public static String getFilename(String path){  
  26.         return path.substring(path.lastIndexOf('/')+1);  
  27.     }  
  28.     /** 
  29.      * 下载文件 
  30.      * @param path 下载路径 
  31.      * @param threadsize 线程数 
  32.      */  
  33.     public void download(String path, int threadsize) throws Exception{  
  34.         URL url = new URL(path);  
  35.         HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
  36.         conn.setRequestMethod("GET");  
  37.         conn.setConnectTimeout(5 * 1000);  
  38.         int filelength = conn.getContentLength();//获取要下载的文件的长度  
  39.         String filename = getFilename(path);//从路径中获取文件名称  
  40.         File saveFile = new File(filename);  
  41.         RandomAccessFile accessFile = new RandomAccessFile(saveFile, "rwd");  
  42.         accessFile.setLength(filelength);//设置本地文件的长度和下载文件相同  
  43.         accessFile.close();  
  44.         //计算每条线程下载的数据长度  
  45.         int block = filelength%threadsize==0? filelength/threadsize : filelength/threadsize+1;  
  46.         for(int threadid=0 ; threadid < threadsize ; threadid++){  
  47.             new DownloadThread(url, saveFile, block, threadid).start();  
  48.         }  
  49.     }  
  50.       
  51.     private final class DownloadThread extends Thread{  
  52.         private URL url;  
  53.         private File saveFile;  
  54.         private int block;//每条线程下载的数据长度  
  55.         private int threadid;//线程id  
  56.   
  57.         public DownloadThread(URL url, File saveFile, int block, int threadid) {  
  58.             this.url = url;  
  59.             this.saveFile = saveFile;  
  60.             this.block = block;  
  61.             this.threadid = threadid;  
  62.         }  
  63.   
  64.         @Override  
  65.         public void run() {  
  66.             //计算开始位置公式:线程id*每条线程下载的数据长度= ?  
  67.             //计算结束位置公式:(线程id +1)*每条线程下载的数据长度-1 =?  
  68.             int startposition = threadid * block;  
  69.             int endposition = (threadid + 1 ) * block - 1;  
  70.             try {  
  71.                 RandomAccessFile accessFile = new RandomAccessFile(saveFile, "rwd");  
  72.                 accessFile.seek(startposition);//设置从什么位置开始写入数据  
  73.                 HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
  74.                 conn.setRequestMethod("GET");  
  75.                 conn.setConnectTimeout(5 * 1000);  
  76.                 conn.setRequestProperty("Range""bytes="+ startposition+ "-"+ endposition);  
  77.                 InputStream inStream = conn.getInputStream();  
  78.                 byte[] buffer = new byte[1024];  
  79.                 int len = 0;  
  80.                 while( (len=inStream.read(buffer)) != -1 ){  
  81.                     accessFile.write(buffer, 0, len);  
  82.                 }  
  83.                 inStream.close();  
  84.                 accessFile.close();  
  85.                 System.out.println("线程id:"+ threadid+ "下载完成");  
  86.             } catch (Exception e) {  
  87.                 e.printStackTrace();  
  88.             }  
  89.         }         
  90.     }  
  91.   
  92. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值