多線程下載文件的思路:
1.首先獲取到文件的總大小
獲取文件大小的方式是通過網(wǎng)絡(luò)讀取,getContentLength()即可獲取到文件的大小,使用RandomAccessFile()支持隨機(jī)訪問
2.根據(jù)所準(zhǔn)備的線程數(shù)據(jù),計(jì)算每一個(gè)線程需要下載的文件的大小
上圖顯示下載400M的電影分4個(gè)線程下載,每一個(gè)線程分別下載各自數(shù)據(jù)段中的數(shù)據(jù),第一個(gè)線程下載0-100M,第二個(gè)下載100M-200M之間的數(shù)據(jù),依次類推。因此下載過程中需要記住的是的開始位置段和結(jié)束位置段,其實(shí)只需要開始位置就可以了,結(jié)束為止可以根據(jù)開始位置加上下載的大小來推斷獲取。
3.獲取到大小數(shù)據(jù)以后,開始用線程循環(huán)讀取每一個(gè)區(qū)間的數(shù)據(jù)
這個(gè)里面需要注意的是,要更新數(shù)據(jù)的寫入位置seek(startIndex),逐段填滿,不然會(huì)出現(xiàn)覆蓋以前的數(shù)據(jù)。
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 ; //線程的個(gè)數(shù) public static void main(String[] args) { // TODO Auto-generated method stub //發(fā)送get請(qǐng)求,請(qǐng)求這個(gè)地址的資源 try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod( "GET" ); conn.setConnectTimeout( 5000 ); conn.setReadTimeout( 5000 ); if (conn.getResponseCode() == 200 ){ //獲取到請(qǐng)求資源文件的長(zhǎng)度 int length = conn.getContentLength(); File file = new File( "QQ.exe" ); //創(chuàng)建隨機(jī)存儲(chǔ)文件 RandomAccessFile raf = new RandomAccessFile(file, "rwd" ); //設(shè)置臨時(shí)文件的大小 raf.setLength(length); //關(guān)閉raf raf.close(); //計(jì)算出每一個(gè)線程下載多少字節(jié) int size = length / Multidownload.ThreadCount; for ( int i = 0 ; i < Multidownload.ThreadCount; i ++){ //startIndex,endIndex分別代表線程的開始和結(jié)束位置 int startIndex = i * size; int endIndex = (i + 1 ) * size - 1 ; if (i == ThreadCount - 1 ){ //如果是最后一個(gè)線程,那么結(jié)束位置寫死 endIndex = length - 1 ; } System.out.println( "線程" + i + "的下載區(qū)間是" + startIndex + "到" + endIndex); new DownLoadThread(startIndex, endIndex, i).start(); //創(chuàng)建線程下載數(shù)據(jù) } } } 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請(qǐng)求下載安裝包文件 URL url; try { url = new URL(Multidownload.path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod( "GET" ); conn.setConnectTimeout( 5000 ); conn.setReadTimeout( 5000 ); //設(shè)置請(qǐng)求數(shù)據(jù)的區(qū)間 conn.setRequestProperty( "Range" , "bytes=" + startIndex + "-" + endIndex); //請(qǐng)求部分?jǐn)?shù)據(jù)的響應(yīng)碼是206 if (conn.getResponseCode() == 206 ){ //獲取一部分?jǐn)?shù)據(jù)來讀取 InputStream is = conn.getInputStream(); byte [] b = new byte [ 1024 ]; int len = 0 ; int total = 0 ; //拿到臨時(shí)文件的引用 File file = new File( "QQ.exe" ); RandomAccessFile raf = new RandomAccessFile(file, "rwd" ); //更新文件的寫入位置,startIndex raf.seek(startIndex); while ((len = is.read(b)) != - 1 ){ //每次讀取流里面的數(shù)據(jù),同步吧數(shù)據(jù)寫入臨時(shí)文件 raf.write(b, 0 , len); total += len; System.out.println( "線程" + threadId + "下載了" + total); } System.out.println( "線程" + threadId + "下載過程結(jié)束===========================" ); raf.close(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }; } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。