java多執行緒實現斷點續傳下載
阿新 • • 發佈:2019-01-01
public class DownloadThread extends Thread {
private int id;
private int startindex;
private int endindex;
private String path;
static int threadfinishedcount=0;
public DownloadThread(String pathurl, int id, int startindex, int endindex) {
super();
this.path=pathurl;
this.id = id;
this.startindex = startindex;
this.endindex = endindex;
}
@Override
public void run() {
try {
String urlpath=path;
URL url=new URL(urlpath);
//獲取網路連線物件
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
//設定請求方式,讀取超時以及連線超時時間
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setReadTimeout(5000);
httpURLConnection.setConnectTimeout(5000);
//-----------------------------------------------定義臨時檔案儲存已下載的長度,檔名為執行緒id.txt
File tempfile=new File(id+".txt");
if(tempfile.exists()){
FileInputStream is=new FileInputStream(tempfile);
BufferedReader br=new BufferedReader(new InputStreamReader(is));
//將臨時檔案中儲存的當前下載的位置付給開始下載位置
startindex=Integer.parseInt(br.readLine());
br.close();
is.close();
}
//-----------------------------------------------
//設定每個執行緒的請求範圍
httpURLConnection.setRequestProperty("Range", "bytes="+startindex+"-"+endindex);
//部分請求成功返回206
if(httpURLConnection.getResponseCode()==206){
//獲取輸入流物件
InputStream inputStream=httpURLConnection.getInputStream();
//讀取長度設定
byte[] b=new byte[1024];
//設定提交檔案的名稱
File file =new File(InternetConnection.getFileName(urlpath));
RandomAccessFile randomAccessFile=new RandomAccessFile(file,"rwd");
//設定從開始位置寫入臨時檔案
randomAccessFile.seek(startindex);
int len=0;
int total=0;
//設定當前下載在的長度
int currentdownloadsize=startindex;
while((len=inputStream.read(b))!=-1){
randomAccessFile.write(b,0,len);
total+=len;
RandomAccessFile temprandRandomAccessFile=new RandomAccessFile(tempfile, "rwd");
//-------------將已經讀取的資料長度按位元組寫入臨時檔案,當前已寫入的長度
currentdownloadsize=total+startindex;
temprandRandomAccessFile.write((currentdownloadsize+"").getBytes());
temprandRandomAccessFile.close();
}
private int id;
private int startindex;
private int endindex;
private String path;
static int threadfinishedcount=0;
public DownloadThread(String pathurl, int id, int startindex, int endindex) {
super();
this.path=pathurl;
this.id = id;
this.startindex = startindex;
this.endindex = endindex;
}
@Override
public void run() {
try {
String urlpath=path;
URL url=new URL(urlpath);
//獲取網路連線物件
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
//設定請求方式,讀取超時以及連線超時時間
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setReadTimeout(5000);
httpURLConnection.setConnectTimeout(5000);
//-----------------------------------------------定義臨時檔案儲存已下載的長度,檔名為執行緒id.txt
File tempfile=new File(id+".txt");
if(tempfile.exists()){
FileInputStream is=new FileInputStream(tempfile);
BufferedReader br=new BufferedReader(new InputStreamReader(is));
//將臨時檔案中儲存的當前下載的位置付給開始下載位置
startindex=Integer.parseInt(br.readLine());
br.close();
is.close();
}
//-----------------------------------------------
//設定每個執行緒的請求範圍
httpURLConnection.setRequestProperty("Range", "bytes="+startindex+"-"+endindex);
//部分請求成功返回206
if(httpURLConnection.getResponseCode()==206){
//獲取輸入流物件
InputStream inputStream=httpURLConnection.getInputStream();
//讀取長度設定
byte[] b=new byte[1024];
//設定提交檔案的名稱
File file =new File(InternetConnection.getFileName(urlpath));
RandomAccessFile randomAccessFile=new RandomAccessFile(file,"rwd");
//設定從開始位置寫入臨時檔案
randomAccessFile.seek(startindex);
int len=0;
int total=0;
//設定當前下載在的長度
int currentdownloadsize=startindex;
while((len=inputStream.read(b))!=-1){
randomAccessFile.write(b,0,len);
total+=len;
RandomAccessFile temprandRandomAccessFile=new RandomAccessFile(tempfile, "rwd");
//-------------將已經讀取的資料長度按位元組寫入臨時檔案,當前已寫入的長度
currentdownloadsize=total+startindex;
temprandRandomAccessFile.write((currentdownloadsize+"").getBytes());
temprandRandomAccessFile.close();
}