Java多線程下載的實現方法_java

來源:互聯網
上載者:User

複製代碼 代碼如下:

package cn.me.test;

import java.io.InputStream;

import java.io.RandomAccessFile;

import java.net.HttpURLConnection;

import java.net.URL;

/**

 * 多線程下載

 * 1:使用RandomAccessFile在任意的位置寫入資料。

 * 2:需要計算第一個線程下載的資料量,可以平均分配。如果不夠平均時,

 *    則直接最後一個線程處理相對較少的資料

 * 3:必須要在下載之前準備好相同大小的檔案,通過檔案頭擷取

 */

public class MultiThreadDownload {

    public static void main(String[] args) throws Exception {

       //1:聲明檔案名稱和下載的地址

       String fileName = "aa.rar";

       String urlStr = "http://localhost:7777/day18";

       //2:聲明Url

       URL url = new URL(urlStr+"/"+fileName);

       //3:擷取串連

       HttpURLConnection con =

           (HttpURLConnection) url.openConnection();

       //4:佈建要求方式

       con.setRequestMethod("GET");

       //5:擷取要求標頭,即檔案的長度

       int length = con.getContentLength();//擷取下載檔案的長度,以計算每個線程應該下載的資料量。

       //6:在指定的目錄下,建立一個同等大小的檔案

       RandomAccessFile file = new RandomAccessFile("d:/a/"+fileName, "rw");//建立一個相同大小的檔案。

       //7:設定檔案大小,佔位

       file.setLength(length);//設定檔案大小。

 

       file.close();

       //8:定義線程個數

       int size = 3;

       //9:計算每一個線程應該下載多少位元組的資料,如果正好整除則最好,否則加1

       int block = length/size==0?length/size:length/size+1;//計算每個線程應該下載的資料量。


       System.err.println("每個線程應該下載:"+block);

       //10:運行三個線程並計算從哪個位元組開始到哪一個位元組結束

       for(int i=0;i<size;i++){

           int start = i*block;

           int end = start+(block-1);//計算每一個線程的開始和結束位元組。

 

         System.err.println(i+"="+start+","+end);

           new MyDownThread(fileName, start, end,url).start();

       }

    }

    static class MyDownThread extends Thread{

       //定義檔案名稱

       private String fileName;

       //定義從何地開始下載

       private int start;

       //定義下載到哪一個位元組

       private int end;

       private URL url;

       public MyDownThread(String fileName,int start,int end,URL url){

           this.fileName=fileName;

           this.start=start;

           this.end=end;

           this.url=url;

       }

       @Override

       public void run() {

           try{

              //11:開始下載

              HttpURLConnection con =

                     (HttpURLConnection) url.openConnection();

              con.setRequestMethod("GET");

              //12:設定分段下載的要求標頭

              con.setRequestProperty("Range","bytes="+start+"-"+end);//設定從伺服器上讀取的檔案塊。

 

              //13:開始下載,需要判斷206

              if(con.getResponseCode()==206){//訪問成功,則返回的狀態代碼為206。

                  InputStream in = con.getInputStream();

                  //14:聲明隨機寫檔案對象,注意rwd是指即時將資料寫到檔案中,而不使用緩衝區

                  RandomAccessFile out = new RandomAccessFile("d:/a/"+fileName,"rwd");

                  out.seek(start);//設定從檔案的某個位置開始寫資料。

                  byte[] b=new byte[1024];

                  int len = 0;

                  while((len=in.read(b))!=-1){

                     out.write(b,0,len);

                  }

                  out.close();

                  in.close();

              }

              System.err.println(this.getName()+"執行完成");

           }catch(Exception e){

              throw new RuntimeException(e);

           }

       }

    }

}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.