package com.itheima.download;java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;緩存
public class MulitDownload {
//設置線程數量
static int thradCount =3;
//設置線程數量讀取
static int finishedThread =0;
//1.設置獲取下載地址Path
static String path ="http://192.168.108.2:8080/WPS.exe";
public static void main(String[] args) {網絡
//2.設置URL偵聽
try {
URL url = new URL(path);
//3.設置創建網絡鏈接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//4.設置請求方式
conn.setRequestMethod("GET");
//設置響應時長和返回時長
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
//5.創建網絡鏈接
conn.connect();
//6.判斷響應碼是否爲200
if(conn.getResponseCode() ==200){
//7.獲取下載文件大小
int length = conn.getContentLength();
//設置保存位置
File file = new File(getFileName(path));
//8.設置緩存
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
//設置臨時文件大小
raf.setLength(length);
raf.close();
//9.設置每一個線程讀取多少
int size = length / thradCount;
for( int i =0; i<thradCount ;i++){
int startIndex = i * size;
int endIndex = (i+1) * size -1;
if(i == thradCount-1){
endIndex = length -1;
}
System.out.println("線程數"+i+"開始區間"+startIndex+"--"+endIndex);
new DownloadThrad(startIndex, endIndex, i).start();
}
dom
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}ide
public static String getFileName(String path){
int index = path.lastIndexOf("/");
return path.substring(index +1);
}
}
class DownloadThrad extends Thread{
int startIndex;
int endIndex;
int threadId;this
public DownloadThrad(int startIndex, int endIndex, int threadId) {
super();
this.startIndex = startIndex;
this.endIndex = endIndex;
this.threadId = threadId;
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
URL url;
try {
//設置斷點續傳ID
File progressFile = new File(threadId+".txt");
if(progressFile.exists()){
FileInputStream fis = new FileInputStream(progressFile);
BufferedReader bf = new BufferedReader(new InputStreamReader(fis));
startIndex += Integer.valueOf(bf.readLine());
}
System.out.println("線程"+threadId+"開始節點"+startIndex+"後續"+endIndex);
url = new URL(MulitDownload.path);
//3.設置創建網絡鏈接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//4.設置請求方式
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(MulitDownload.getFileName(MulitDownload.path));
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
//指定從哪一個節點開始
raf.seek(startIndex);
while((len = is.read(b))!=-1){
raf.write(b, 0, len);
total +=len;
System.out.println("當前線程"+threadId+"下載"+total);
url
//設置緩存讀取
RandomAccessFile progressRaf = new RandomAccessFile(progressFile, "rwd");
//設置寫入文本信息
progressRaf.write((total+"").getBytes());
progressRaf.close();
}
raf.close();
//設置讀取線程 數量
MulitDownload.finishedThread++;
//同步語句
synchronized (MulitDownload.path) {
if(MulitDownload.finishedThread == MulitDownload.thradCount){
for(int i=0;i<MulitDownload.thradCount;i++){
File f = new File( i +".txt");
f.delete();
System.out.println("打印當前"+i);
}
//設置判斷爲0
MulitDownload.finishedThread =0;
}.net
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}線程
}orm
}
---------------------
做者:其實不懂噯
來源:CSDN
原文:https://blog.csdn.net/u014248897/article/details/76039725
版權聲明:本文爲博主原創文章,轉載請附上博文連接!
https://blog.csdn.net/u014248897/article/details/76039725