java多线程下载的实现示例





[java]  view plain  copy
  1. import java.io.File;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.InputStream;  
  4. import java.io.RandomAccessFile;  
  5. import java.net.HttpURLConnection;  
  6. import java.net.URL;  
  7.   
  8.   
  9. public class Main {  
  10.   
  11.     /** 
  12.      * @param args 
  13.      * @throws Exception  
  14.      */  
  15.     public static void main(String[] args) throws Exception {  
  16.         new Main().download("http://gdown.baidu.com/data/wisegame/a3d96bf8e8a5102c/baiduliulanqi_25.apk"5);  
  17.     }  
  18.       
  19.     public void download(String downPath, int threadNum) throws Exception{  
  20.         URL url = new URL(downPath);  
  21.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  22.         conn.setConnectTimeout(5000);  
  23.         conn.setRequestMethod("GET");  
  24.         //InputStream in = conn.getInputStream();  
  25.           
  26.         if(conn.getResponseCode() == 200){  
  27.             int size = conn.getContentLength();  
  28.             File file = new File(downPath.substring(downPath.lastIndexOf("/")+1));  
  29.               
  30.             //"rwd" 会立刻把数据同步到设备上,防止手机没电数据丢失  
  31.             RandomAccessFile raf = new RandomAccessFile(file, "rwd");  
  32.             raf.setLength(size); //暂时还没数据  
  33.             raf.close();  
  34.               
  35.             int block = size%threadNum == 0 ? size/threadNum : size/threadNum + 1;  
  36.             for(int i=0; i<threadNum; i++){  
  37.                 new DownThread(i, block, url, file).start();  
  38.             }  
  39.               
  40.         }else{  
  41.             System.out.println("下载失败");  
  42.         }  
  43.           
  44.     }  
  45.       
  46.     //下载线程类  
  47.     private class DownThread extends Thread{  
  48.         private int id;  
  49.         private int block; //每个线程下载块大小  
  50.         private URL url; //地址  
  51.         private File file; //下载保存在哪个文件  
  52.           
  53.         public DownThread(int i, int block, URL url, File file) {  
  54.             this.id = i;  
  55.             this.block = block;  
  56.             this.url = url;  
  57.             this.file = file;  
  58.         }  
  59.           
  60.         public void run(){  
  61.             int start = id * block;  
  62.             int end = (id+1) * block - 1;  
  63.               
  64.             try {  
  65.                 RandomAccessFile raf = new RandomAccessFile(file, "rwd");  
  66.                 raf.seek(start);  
  67.                 HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  68.                 conn.setConnectTimeout(5000);  
  69.                 conn.setRequestMethod("GET");  
  70.                   
  71.                 //通过头字段,指定获取文件的位置 bytes="1-1000"  
  72.                 conn.setRequestProperty("Range""bytes=" + start + "-" + end);  
  73.                   
  74.                 //分段请求文件时,返回码是206  
  75.                 if(conn.getResponseCode() == 206){  
  76.                     byte[] buffer = new byte[1024];  
  77.                     InputStream in = conn.getInputStream();  
  78.                     int len = 0;  
  79.                     while( (len=in.read(buffer)) != -1){  
  80.                         raf.write(buffer, 0, len);  
  81.                     }  
  82.                     raf.close();  
  83.                     in.close();  
  84.   
  85.                     System.out.println("线程:" + id + "下载完成");  
  86.                 }else{  
  87.                     System.out.println("失败");  
  88.                 }  
  89.                   
  90.             } catch (Exception e) {  
  91.                 e.printStackTrace();  
  92.             }  
  93.             System.out.println("");  
  94.         }  
  95.           
  96.     }  
  97.   
  98. }  



[java]  view plain  copy
  1. import java.io.File;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.InputStream;  
  4. import java.io.RandomAccessFile;  
  5. import java.net.HttpURLConnection;  
  6. import java.net.URL;  
  7.   
  8.   
  9. public class Main {  
  10.   
  11.     /** 
  12.      * @param args 
  13.      * @throws Exception  
  14.      */  
  15.     public static void main(String[] args) throws Exception {  
  16.         new Main().download("http://gdown.baidu.com/data/wisegame/a3d96bf8e8a5102c/baiduliulanqi_25.apk"5);  
  17.     }  
  18.       
  19.     public void download(String downPath, int threadNum) throws Exception{  
  20.         URL url = new URL(downPath);  
  21.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  22.         conn.setConnectTimeout(5000);  
  23.         conn.setRequestMethod("GET");  
  24.         //InputStream in = conn.getInputStream();  
  25.           
  26.         if(conn.getResponseCode() == 200){  
  27.             int size = conn.getContentLength();  
  28.             File file = new File(downPath.substring(downPath.lastIndexOf("/")+1));  
  29.               
  30.             //"rwd" 会立刻把数据同步到设备上,防止手机没电数据丢失  
  31.             RandomAccessFile raf = new RandomAccessFile(file, "rwd");  
  32.             raf.setLength(size); //暂时还没数据  
  33.             raf.close();  
  34.               
  35.             int block = size%threadNum == 0 ? size/threadNum : size/threadNum + 1;  
  36.             for(int i=0; i<threadNum; i++){  
  37.                 new DownThread(i, block, url, file).start();  
  38.             }  
  39.               
  40.         }else{  
  41.             System.out.println("下载失败");  
  42.         }  
  43.           
  44.     }  
  45.       
  46.     //下载线程类  
  47.     private class DownThread extends Thread{  
  48.         private int id;  
  49.         private int block; //每个线程下载块大小  
  50.         private URL url; //地址  
  51.         private File file; //下载保存在哪个文件  
  52.           
  53.         public DownThread(int i, int block, URL url, File file) {  
  54.             this.id = i;  
  55.             this.block = block;  
  56.             this.url = url;  
  57.             this.file = file;  
  58.         }  
  59.           
  60.         public void run(){  
  61.             int start = id * block;  
  62.             int end = (id+1) * block - 1;  
  63.               
  64.             try {  
  65.                 RandomAccessFile raf = new RandomAccessFile(file, "rwd");  
  66.                 raf.seek(start);  
  67.                 HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  68.                 conn.setConnectTimeout(5000);  
  69.                 conn.setRequestMethod("GET");  
  70.                   
  71.                 //通过头字段,指定获取文件的位置 bytes="1-1000"  
  72.                 conn.setRequestProperty("Range""bytes=" + start + "-" + end);  
  73.                   
  74.                 //分段请求文件时,返回码是206  
  75.                 if(conn.getResponseCode() == 206){  
  76.                     byte[] buffer = new byte[1024];  
  77.                     InputStream in = conn.getInputStream();  
  78.                     int len = 0;  
  79.                     while( (len=in.read(buffer)) != -1){  
  80.                         raf.write(buffer, 0, len);  
  81.                     }  
  82.                     raf.close();  
  83.                     in.close();  
  84.   
  85.                     System.out.println("线程:" + id + "下载完成");  
  86.                 }else{  
  87.                     System.out.println("失败");  
  88.                 }  
  89.                   
  90.             } catch (Exception e) {  
  91.                 e.printStackTrace();  
  92.             }  
  93.             System.out.println("");  
  94.         }  
  95.           
  96.     }  
  97.   
  98. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值