1. 程式人生 > >網絡多線線程下載示例

網絡多線線程下載示例

index exists extend reat multi xtend int main leo

網絡多線線程下載:

IO流與多線程應用示例

  1 import priv.gdw.utils.FileUtils;
  2 import java.io.*;
  3 import java.net.URL;
  4 import java.net.URLConnection;
  5 
  6 /**
  7  * Created by kong on 07/11/2017.
  8  */
  9  
 10 public class Test02 {
 11 
 12     public static void main(String[] args) throws IOException {
 13
multithreadedDownload ("http://localhost:8080/image/a.jpg","/Users/Shared/abc",3); 14 } 15 16 /** 17 * 多線程下載,僅適用於Http協議 18 * 19 * @param srcPath 下載源文件路徑名 20 * @param destPath 保存文件夾路徑名 21 * @param count 指定線程個數 22 */ 23 public static void multithreadedDownload(String srcPath, String destPath, int
count) throws IOException { 24 25 //定義一個文件下載的文件夾路徑 26 File dir = new File (destPath); 27 if (!dir.exists ()) { 28 dir.mkdirs (); 29 } 30 31 long total = getFileLength (srcPath); 32 OpenMultithreadedDownload (srcPath, dir, total, count);
33 34 //判斷線程的數量,如果只剩下一個,說明其他線程已經下載完成了 35 while (Thread.activeCount () != 1) { 36 try { 37 Thread.sleep (10); 38 } catch (InterruptedException e) { 39 e.printStackTrace (); 40 } 41 } 42 //獲取文件的後綴名 43 String suffix = srcPath.substring (srcPath.lastIndexOf (".")); 44 //合並文件 45 FileUtils.merge (dir, suffix); 46 47 System.out.println ("文件下載完成"); 48 } 49 50 /** 51 * @param count 指定線程個數 52 */ 53 private static void OpenMultithreadedDownload(String path, File dir, long total, int count) { 54 55 //計算出每個線程平均現在的字節個數 56 long size = total / count; 57 //使用循環,計算每個線程下載開始和結束位置 58 for (int i = 1; i <= count; i++) { 59 //開始位置 60 long starIndex = (i - 1) * size; 61 //結束位置 62 long endIndex = i * size - 1; 63 //最後一個線程 64 if (i == count) { 65 endIndex = total - 1; 66 } 67 DownloadThread dt = new DownloadThread (path, dir, starIndex, endIndex, i); 68 dt.start (); 69 } 70 } 71 72 //獲取到服務器上的文件大小 73 public static long getFileLength(String path) throws IOException { 74 75 URL url = new URL (path); 76 //獲得連接對象 77 URLConnection con = url.openConnection (); 78 long total = con.getContentLengthLong (); 79 return total; 80 } 81 } 82 83 84 class DownloadThread extends Thread { 85 86 private final String path; 87 private final File dir; 88 private final long startIndex; 89 private final long endIndex; 90 private final int threadID; 91 92 public DownloadThread(String path, File dir, long startIndex, 93 long endIndex, int threadID) { 94 this.path = path; 95 this.dir = dir; 96 this.startIndex = startIndex; 97 this.endIndex = endIndex; 98 this.threadID = threadID; 99 } 100 @Override 101 public void run() { 102 try { 103 InputStream in = getInputStream (); 104 FileOutputStream fos = getFileOutputStream (); 105 int len ; 106 byte[] arr = new byte[8192]; 107 while ((len = in.read (arr)) != -1){ 108 fos.write (arr,0,len); 109 } 110 in.close (); 111 fos.close (); 112 } catch (Exception e) { 113 e.printStackTrace (); 114 } 115 116 } 117 //獲取下載輸出流 118 private FileOutputStream getFileOutputStream() throws FileNotFoundException { 119 //獲取到文件的名字 120 int index = path.lastIndexOf ("/")+1; 121 String name = path.substring (index); 122 File file = new File (dir,threadID+name);//創建目標文件鍵,防止文件覆蓋 123 //創建一個輸出流 124 return new FileOutputStream (file); 125 } 126 //獲取下載的輸入流 127 private InputStream getInputStream() throws IOException { 128 //創建一個URL對象 129 URL url= new URL (path); 130 //獲取到連接對象 131 URLConnection con = url.openConnection (); 132 con.setRequestProperty ("Range","bytes="+startIndex+"-"+endIndex); 133 //獲取到輸入流 134 return con.getInputStream (); 135 } 136 }

合並文件功能塊:

 1 package priv.gdw.utils;
 2 
 3 import java.io.*;
 4 
 5 public class FileUtils {
 6 
 7     //合並文件功能
 8     public static void merge(File dir,String suffix) throws IOException {
 9 
10         File[] files = dir.listFiles (FileUtils.getFileFilter (suffix,false));
11         FileOutputStream fos = getFileOutputStream (dir, files[0]);
12 
13         for(File file : files){
14             FileInputStream fis = new FileInputStream (file);
15             int len;
16             byte[] buf = new byte[8192];
17             while ((len=fis.read (buf)) != -1){
18                 fos.write (buf,0,len);
19             }
20             fis.close ();
21             //把已經合並過的文件刪除
22             file.delete ();
23         }
24         fos.close ();
25     }
26     //獲取合並文件輸出流
27     private static FileOutputStream getFileOutputStream(File dir, File file) throws FileNotFoundException {
28         File file1 = file;
29         String name = file1.getName ();
30         name = name.substring (1);
31         File destFile = new File (dir,name);
32         return new FileOutputStream (destFile);
33     }
34 
35 }

網絡多線線程下載示例