使用多线程(http协议)实现下载功能(java)

代码附上

 

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

 

 

 

 

 

posted @ 2012-03-04 09:55  南洋小学生  Views(640)  Comments(0)    收藏  举报