JAVA記憶體對映檔案實現多執行緒下載

壹頁書發表於2014-05-15
JAVA NIO記憶體對映檔案可以實現多執行緒下載
首先,使用firefox下載一個tomcat


JAVA多執行緒下載程式

  1. import java.io.FileNotFoundException;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.RandomAccessFile;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7. import java.nio.MappedByteBuffer;
  8. import java.nio.channels.FileChannel.MapMode;

  9. class Worker implements Runnable {
  10.     //多執行緒下載的數量
  11.     private static int THREADS = 4;
  12.     //每個執行緒下載開始的位置
  13.     private int startIndex;
  14.     //每個執行緒下載內容的長度
  15.     private int length;
  16.     //檔案儲存位置
  17.     private String localFile;
  18.     //遠端檔案的流
  19.     InputStream in;

  20.     private Worker(String urlFile, String localFile, int startIndex, int length) throws IOException {
  21.         this.startIndex = startIndex;
  22.         this.length = length;
  23.         this.localFile = localFile;
  24.         init(urlFile);
  25.     }

  26.     /**
  27.      *    主執行緒開啟網路檔案,先分割為指定的大小,然後開啟多執行緒下載
  28.      */
  29.     public Worker(String urlFile, String localFile) throws IOException {
  30.         this.localFile = localFile;
  31.         int contentLength = init(urlFile);
  32.         int step = contentLength / THREADS;
  33.         int index = 0;
  34.         for (int i = 0; i < THREADS; i++) {
  35.             if (i == 0) {
  36.                 this.startIndex = 0;
  37.                 this.length = step;
  38.                 new Thread(this).start();
  39.             } else if (i == THREADS - 1) {
  40.                 Worker worker = new Worker(urlFile, localFile, index, contentLength - index);
  41.                 new Thread(worker).start();
  42.             } else {
  43.                 Worker worker = new Worker(urlFile, localFile, index, step);
  44.                 new Thread(worker).start();
  45.             }
  46.             index = index + step;
  47.         }
  48.     }

  49.     private int init(String urlFile) throws IOException {
  50.         URL url;

  51.         url = new URL(urlFile);

  52.         HttpURLConnection connection = (HttpURLConnection) url.openConnection();

  53.         connection.setConnectTimeout(5 * 1000);
  54.         connection.setRequestMethod("GET");
  55.         connection.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, " + "application/x-shockwave-flash, application/xaml+xml, "
  56.                 + "application/vnd.ms-xpsdocument, application/x-ms-xbap, " + "application/x-ms-application, application/vnd.ms-excel, " + "application/vnd.ms-powerpoint, application/msword, */*");
  57.         connection.setRequestProperty("Accept-Language", "zh-CN");
  58.         connection.setRequestProperty("Charset", "UTF-8");
  59.         connection.setRequestProperty("Connection", "Keep-Alive");
  60.         InputStream in = connection.getInputStream();
  61.         this.in = in;
  62.         return connection.getContentLength();
  63.     }

  64.     @Override
  65.     public void run() {
  66.         System.out.println(this);
  67.         try {
  68.             RandomAccessFile localRandomFile = new RandomAccessFile(localFile, "rw");
  69.             MappedByteBuffer buffer = localRandomFile.getChannel().map(MapMode.READ_WRITE, startIndex, length);
  70.             int i = 0;
  71.             in.skip(startIndex);
  72.             while (i < length) {
  73.                 buffer.put((byte) in.read());
  74.                 i++;
  75.             }
  76.             buffer.force();
  77.             in.close();
  78.             localRandomFile.close();
  79.         } catch (FileNotFoundException e) {
  80.             e.printStackTrace();
  81.         } catch (IOException e) {
  82.             e.printStackTrace();
  83.         }
  84.     }

  85.     @Override
  86.     public String toString() {
  87.         return "Worker [localFile=" + localFile + ", startIndex=" + startIndex + ", length=" + length + "]";
  88.     }

  89.     public static void main(String[] args) throws IOException {
  90.         new Worker("http://mirrors.cnnic.cn/apache/tomcat/tomcat-7/v7.0.53/bin/apache-tomcat-7.0.53.zip", "tomcat.zip");
  91.     }

  92. }

比對下載的檔案


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-1162157/,如需轉載,請註明出處,否則將追究法律責任。

相關文章