package com.briup.day24;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
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 DownloadThread extends Thread {
private static final Object o=new Object();
private int id;
private String path;
private int startIndex;
private int endIndex;
public DownloadThread(int id, String path, int startIndex, int endIndex) {
// TODO Auto-generated constructor stub
this.id = id;
this.path = path;
this.startIndex = startIndex;
this.endIndex = endIndex;
}
@Override
public void run() {
// TODO Auto-generated method stub
URL url;
try {
File file = new File(id + ".txt");
int lastProgress = 0;
if (file.exists()) {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
lastProgress = Integer.parseInt(br.readLine());
startIndex+=lastProgress;
br.close();
}
url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(8000);
connection.setConnectTimeout(8000);
connection.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex);
connection.connect();
if (connection.getResponseCode() == HttpURLConnection.HTTP_PARTIAL) {
InputStream inputStream = connection.getInputStream();
byte[] arr = new byte[1024];
int len = -1;
int total = lastProgress;
RandomAccessFile raf = new RandomAccessFile("download.jpg", "rwd");
raf.seek(startIndex);
RandomAccessFile raff = null;
while ((len = inputStream.read(arr)) != -1) {
// 写入到本地文件中
sleep(100);
raf.write(arr, 0, len);
total += len;
System.out.println("第" + id + "条线程下载了" + total);
raff = new RandomAccessFile(file, "rwd");
raff.write((total + "").getBytes());
}
raff.close();
raf.close();
synchronized (o) {
Main.finishedThreadCount++;
if(Main.finishedThreadCount==Main.THREADCOUNT) {
//删除
for(int i=0;i<Main.THREADCOUNT;i++) {
File f=new File(i+".txt");
f.delete();
}
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.briup.day24;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
/**
- 多线程断点下载 1、先去磁盘上创建一个要下载的文件大小一样的空文件 2、计算每个线程的下载区间
-
@author yuxiangrousigaifan
*
*/
public class Main {public static final String PATH = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1508931223542&di=69e33a551ab94504d1124b9ba28ab3c2&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fimgad%2Fpic%2Fitem%2F0bd162d9f2d3572c329d50f18113632762d0c3a4.jpg";
public static final int THREADCOUNT = 3;public static int finishedThreadCount = 0;
public static void main(String[] args) throws IOException {
URL url = new URL(PATH); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(8000); connection.setConnectTimeout(8000); connection.connect(); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { int size = connection.getContentLength(); System.out.println(size); File file = new File("download.jpg"); RandomAccessFile raf = new RandomAccessFile(file, "rwd"); raf.setLength(size); raf.close(); // int length = size / THREADCOUNT; for (int id = 0; id < THREADCOUNT; id++) { int startIndex = id * length; int endIndex = (id + 1) * length - 1; if (id == THREADCOUNT - 1) { endIndex = size - 1; } System.out.println("编号为" + id + "的线程的现在区间是" + startIndex + "----" + endIndex); new DownloadThread(id, PATH, startIndex, endIndex).start(); } }
}
}
用这个程序下载之后 在工程下面一直保留三个临时下载记录 删除不掉 ,但是我在线程类最后用了delete()方法呀 求大佬们解答