多線程下載

/**

* 多線程下載器

*/

public class MultiDownloader {

/**

* 開啟幾個線程從服務器下載數據

*/

public static int threadCount = 3;

public static int runningThreadCount ;

// 服務器的文件,準備出來,tomcat服務器上.

public static String path = "http://192.168.1.104:8080/setup.exe";

// 多線程下載

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

URL url = new URL(path);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setConnectTimeout(5000);

conn.setRequestMethod("GET");

int code = conn.getResponseCode();

if (code == 200) {

int length = conn.getContentLength();

System.out.println("服務器文件的長度為:" + length);

RandomAccessFile raf = new RandomAccessFile(getFileName(path), "rw");

raf.setLength(length);

raf.close();

int blocksize = length / threadCount;

runningThreadCount = threadCount;

for (int threadId = 0; threadId < threadCount; threadId++) {

int startIndex = threadId * blocksize;

int endIndex = (threadId + 1) * blocksize - 1;

if (threadId == (threadCount - 1)) {

endIndex = length - 1;

}

new DownloadThread(threadId, startIndex, endIndex).start();

}

}

}

private static class DownloadThread extends Thread {

/**

* 線程id

*/

private int threadId;

/**

* 線程下載的理論開始位置

*/

private int startIndex;

/**

* 線程下載的結束位置

*/

private int endIndex;

/**

* 當前線程下載到文件的那一個位置了.

*/

private int currentPosition;

public DownloadThread(int threadId, int startIndex, int endIndex) {

this.threadId = threadId;

this.startIndex = startIndex;

this.endIndex = endIndex;

System.out.println(threadId + "號線程下載的范圍為:" + startIndex

+ "? ~~? " + endIndex);

currentPosition = startIndex;

}

@Override

public void run() {

try {

URL url = new URL(path);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

//檢查當前線程是否已經下載過一部分的數據了

File info = new File(threadId+".position");

RandomAccessFile raf = new RandomAccessFile(getFileName(path), "rw");

if(info.exists()&&info.length()>0){

FileInputStream fis = new FileInputStream(info);

BufferedReader br = new BufferedReader(new InputStreamReader(fis));

currentPosition = Integer.valueOf(br.readLine());

conn.setRequestProperty("Range", "bytes="+currentPosition+"-"+endIndex);

System.out.println("原來有下載進度,從上一次終止的位置繼續下載"+"bytes="+currentPosition+"-"+endIndex);

fis.close();

raf.seek(currentPosition);//每個線程寫文件的開始位置都是不一樣的.

}else{

//告訴服務器 只想下載資源的一部分

conn.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex);

System.out.println("原來沒有有下載進度,新的下載"+ "bytes="+startIndex+"-"+endIndex);

raf.seek(startIndex);//每個線程寫文件的開始位置都是不一樣的.

}

InputStream is = conn.getInputStream();

byte[] buffer = new byte[1024*1024*10];

int len = -1;

while((len = is.read(buffer))!=-1){

//把每個線程下載的數據放在自己的空間里面.

// System.out.println("線程:"+threadId+"正在下載:"+new String(buffer));

raf.write(buffer,0, len);

currentPosition+=len;

File file = new File(threadId+".position");

RandomAccessFile fos = new RandomAccessFile(file,"rwd");

//System.out.println("線程:"+threadId+"寫到了"+currentPosition);

fos.write(String.valueOf(currentPosition).getBytes());

fos.close();//fileoutstream數據是不一定被寫入到底層設備里面的,有可能是存儲在緩存里面.

//raf 的 rwd模式,數據是立刻被存儲到底層硬盤設備里面.

}

raf.close();

is.close();

System.out.println("線程:"+threadId+"下載完畢了...");

File f = new File(threadId+".position");

f.renameTo(new File(threadId+".position.finish"));

synchronized (MultiDownloader.class) {

runningThreadCount--;

if(runningThreadCount<=0){

for(int i=0;i

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容