多線程下載文件的思路:
1.首先獲取到文件的總大小
獲取文件大小的方式是通過網絡讀取,getContentLength()即可獲取到文件的大小,使用RandomAccessFile()支持隨機訪問
2.根據所準備的線程數據,計算每一個線程需要下載的文件的大小
上圖顯示下載400M的電影分4個線程下載,每一個線程分別下載各自數據段中的數據,第一個線程下載0-100M,第二個下載100M-200M之間的數據,依次類推。因此下載過程中需要記住的是的開始位置段和結束位置段,其實只需要開始位置就可以了,結束為止可以根據開始位置加上下載的大小來推斷獲取。
3.獲取到大小數據以后,開始用線程循環讀取每一個區間的數據
這個里面需要注意的是,要更新數據的寫入位置seek(startIndex),逐段填滿,不然會出現覆蓋以前的數據。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
package com.ldw.multilthreaddownload; import java.io.File; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL; public class Multidownload { static int ThreadCount = 3 ; //線程的個數 public static void main(String[] args) { // TODO Auto-generated method stub //發送get請求,請求這個地址的資源 try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod( "GET" ); conn.setConnectTimeout( 5000 ); conn.setReadTimeout( 5000 ); if (conn.getResponseCode() == 200 ){ //獲取到請求資源文件的長度 int length = conn.getContentLength(); File file = new File( "QQ.exe" ); //創建隨機存儲文件 RandomAccessFile raf = new RandomAccessFile(file, "rwd" ); //設置臨時文件的大小 raf.setLength(length); //關閉raf raf.close(); //計算出每一個線程下載多少字節 int size = length / Multidownload.ThreadCount; for ( int i = 0 ; i < Multidownload.ThreadCount; i ++){ //startIndex,endIndex分別代表線程的開始和結束位置 int startIndex = i * size; int endIndex = (i + 1 ) * size - 1 ; if (i == ThreadCount - 1 ){ //如果是最后一個線程,那么結束位置寫死 endIndex = length - 1 ; } System.out.println( "線程" + i + "的下載區間是" + startIndex + "到" + endIndex); new DownLoadThread(startIndex, endIndex, i).start(); //創建線程下載數據 } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } class DownLoadThread extends Thread{ int startIndex; int endIndex; int threadId; public DownLoadThread( int startIndex, int endIndex, int threadId) { super (); this .startIndex = startIndex; this .endIndex = endIndex; this .threadId = threadId; } @Override public void run(){ //使用http請求下載安裝包文件 URL url; try { url = new URL(Multidownload.path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod( "GET" ); conn.setConnectTimeout( 5000 ); conn.setReadTimeout( 5000 ); //設置請求數據的區間 conn.setRequestProperty( "Range" , "bytes=" + startIndex + "-" + endIndex); //請求部分數據的響應碼是206 if (conn.getResponseCode() == 206 ){ //獲取一部分數據來讀取 InputStream is = conn.getInputStream(); byte [] b = new byte [ 1024 ]; int len = 0 ; int total = 0 ; //拿到臨時文件的引用 File file = new File( "QQ.exe" ); RandomAccessFile raf = new RandomAccessFile(file, "rwd" ); //更新文件的寫入位置,startIndex raf.seek(startIndex); while ((len = is.read(b)) != - 1 ){ //每次讀取流里面的數據,同步吧數據寫入臨時文件 raf.write(b, 0 , len); total += len; System.out.println( "線程" + threadId + "下載了" + total); } System.out.println( "線程" + threadId + "下載過程結束===========================" ); raf.close(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }; } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。