多线程下载辅助类实现

来源:互联网 发布:网络公益活动有哪些 编辑:程序博客网 时间:2024/04/30 00:18

如需转载,必须注明转自: http://blog.csdn.net/xiazdong/article/details/7738298


使用声明:

类名:MultiThreadDownloadUtil

API:

(1)download(String path,int count);

path:文件URL;

count:线程数量;

 

[java] view plaincopy
  1. package org.xiazdong.multidownload.util;  
  2.   
  3. import java.io.File;  
  4. import java.io.InputStream;  
  5. import java.io.RandomAccessFile;  
  6. import java.net.HttpURLConnection;  
  7. import java.net.URL;  
  8.   
  9.   
  10. public class MultiThreadDownloadUtil{  
  11.     private static int filesize;  
  12.     private static int block;  
  13.     private static String filename;  
  14.     private static int downloadsize;  
  15.     private class MyThread extends Thread{  
  16.         private int i;          //线程ID  
  17.         private String path;    //下载文件的URL  
  18.   
  19.         public MyThread(int i, String path) {  
  20.             this.i = i;  
  21.             this.path = path;  
  22.             downloadsize = 0;  
  23.         }  
  24.         @Override  
  25.         public void run() {  
  26.             try{  
  27.                 System.out.println("线程"+(i+1)+"开始下载");  
  28.                 //1.打开文件,并定位位置  
  29.                 RandomAccessFile raf = new RandomAccessFile(new File(filename), "rwd");  
  30.                 raf.seek(i*block);          //定位到此线程要负责下载的位置  
  31.                 int start = i*block;  
  32.                 int end = (i+1)*block-1;  
  33.                 //2.连接服务器  
  34.                 URL url = new URL(path);  
  35.                 HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
  36.                 conn.setRequestMethod("GET");  
  37.                 conn.setRequestProperty("range""bytes="+start+"-"+end);   //发出请求头  
  38.                   
  39.                 if(conn.getResponseCode()==206){        //注意:分段下载的返回码为206,而不是200  
  40.                     InputStream in = conn.getInputStream();  
  41.                     int length = 0;  
  42.                     byte[]data = new byte[1024];  
  43.                     while((length=in.read(data))!=-1){  
  44.                         raf.write(data,0,length);       //写入本地文件  
  45.                     }  
  46.                 }  
  47.                 //显示下载进度  
  48.                 downloadsize += (end-start);  
  49.                 System.out.println("已下载"+downloadsize/1024.0+"k,共"+filesize/1024.0+"k");  
  50.                 //3.关闭文件  
  51.                 raf.close();  
  52.                 System.out.println("线程"+(i+1)+"结束下载...");  
  53.             }  
  54.             catch(Exception e){  
  55.                 e.printStackTrace();  
  56.             }  
  57.         }  
  58.     }  
  59.     /** 
  60.      * 下载文件 
  61.      * @param path          URL 
  62.      * @param threadcount   线程数 
  63.      * @throws Exception 
  64.      */  
  65.     public static void download(String path,int threadcount) throws Exception{  
  66.         filename = path.substring(path.lastIndexOf('/')+1);  
  67.         filesize = getFileSize(path);  
  68.         block = getBlockSize(filesize,threadcount);  
  69.         createLocalRandomFile(filesize);  
  70.         MultiThreadDownloadUtil mdu = new MultiThreadDownloadUtil();  
  71.         for(int i=0;i<threadcount;i++){  
  72.             mdu.new MyThread(i,path).start();  
  73.         }  
  74.     }  
  75.     /** 
  76.      * 创建一个本地文件,并设置文件的大小 
  77.      * @param filesize 
  78.      * @throws Exception 
  79.      */  
  80.     private static void createLocalRandomFile(int filesize) throws Exception {  
  81.         RandomAccessFile raf = new RandomAccessFile(new File(filename), "rwd");  
  82.         raf.setLength(filesize);  
  83.         raf.close();  
  84.     }  
  85.     /** 
  86.      * 根据文件总大小和线程数求出每个线程要下载的数据量 
  87.      * @param filesize 
  88.      * @param threadcount 
  89.      * @return 
  90.      */  
  91.     private static int getBlockSize(int filesize, int threadcount) {  
  92.         return filesize%threadcount==0?filesize/threadcount:(filesize/threadcount+1);  
  93.     }  
  94.     /** 
  95.      * 求出文件总大小 
  96.      * @param path 
  97.      * @return 
  98.      * @throws Exception 
  99.      */  
  100.     private static int getFileSize(String path) throws Exception{  
  101.         URL url = new URL(path);  
  102.         HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
  103.         conn.setRequestMethod("GET");  
  104.         if(conn.getResponseCode()==200){  
  105.             return conn.getContentLength();  
  106.         }  
  107.         else{  
  108.             return 0;  
  109.         }  
  110.     }  
  111. }  

 

 

测试类:


[java] view plaincopy
  1. package org.xiazdong.download;  
  2.   
  3. import org.xiazdong.multidownload.util.MultiThreadDownloadUtil;  
  4.   
  5.   
  6. public class MultiThreadDownloader {  
  7.   
  8.     public static void main(String[] args) throws Exception {  
  9.         int threadCount = 3;    //指定线程数量  
  10.         String path = "http://dlc2.pconline.com.cn/filedown_61761_6694063/drivethelife2010_pconline_setup.exe"; //指定下载文件路径  
  11.         MultiThreadDownloadUtil.download(path, threadCount);  
  12.     }  
  13. }  


将此文件下载到工程目录后,运行可用;

原创粉丝点击