1.创建一个大小跟服务器文件相同大小的临时文件
2. 计算分配几个线程去下载服务器上的资源, 知道每个线程下载文件的位置
3. 开启多个线程,每个线程下载对应位置的文件
4.如果所有的线程,都把自己的数据下载完毕了,服务器上的资源就被下载到本地
public static void main(String[] args) throws Exception {
int threadcount = 3;
String path = "http://127.0.0.1:8080/Kw.exe";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if(code == 200){
int length = conn.getContentLength();
System.out.println("length:" + length);
//在本地创建一个相应大小的临时文件
RandomAccessFile raf = new RandomAccessFile("yd.exe", "rwd");
raf.setLength(length);
raf.close();
int blockSize = length / threadcount;
for(int threadid = 1; threadid<=threadcount; threadid++){
int startpositon = (threadid-1)*blockSize;
int endposition = threadid*blockSize - 1;
if(threadid == threadcount){
endposition = length ;
}
new ThreadDownLoad(length, startpositon, endposition, path).start();
System.out.println("第" + threadid + "线程:" + startpositon + "----->" + endposition);
}
}else{
System.out.println("请求失败");
}
}
public static class ThreadDownLoad extends Thread{
private int length;
private int startposition;
private int endposition;
private String path;
public ThreadDownLoad(int length, int startposition, int endposition,
String path) {
super();
this.length = length;
this.startposition = startposition;
this.endposition = endposition;
this.path = path;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("GET");
//重要:请求服务器下载部分的文件指定文件的位置
conn.setRequestProperty("Range", "bytes=" + startposition + "-" + endposition);
InputStream is = conn.getInputStream(); // 已经指定文件的位置,返回相应文件位置的输入流。
RandomAccessFile raf = new RandomAccessFile("yd.exe", "rwd");
//随机写文件的时候从哪个位置开始写
raf.seek(startposition);
int len = 0;
byte[] buffer = new byte[1024];
while( (len = is.read(buffer)) != -1){
raf.write(buffer, 0, len);
}
is.close();
raf.close();
System.out.println(startposition + "----》" + endposition + "完成");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//super.run();
}
}
断点续传:
public static void main(String[] args) throws Exception {
int threadcount = 3;
String path = "http://127.0.0.1:8080/ku.exe";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if(code == 200){
int length = conn.getContentLength();
System.out.println("length:" + length);
//在本地创建一个相应大小的临时文件
RandomAccessFile raf = new RandomAccessFile("ku.exe", "rwd");
raf.setLength(length);
raf.close();
int blockSize = length / threadcount;
for(int threadid = 1; threadid<=threadcount; threadid++){
int startpositon = (threadid-1)*blockSize;
int endposition = threadid*blockSize - 1;
if(threadid == threadcount){
endposition = length ;
}
new ThreadDownLoad(threadid, startpositon, endposition, path).start();
System.out.println("第" + threadid + "线程:" + startpositon + "----->" + endposition);
}
}else{
System.out.println("请求失败");
}
}
public static class ThreadDownLoad extends Thread{
private int threadid;
private int startposition;
private int endposition;
private String path;
public ThreadDownLoad(int threadid, int startposition, int endposition,
String path) {
super();
this.threadid = threadid;
this.startposition = startposition;
this.endposition = endposition;
this.path = path;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
//检查文件是否有记录已经下载过的文件大小
File tempFile = new File(threadid + ".txt");
if(tempFile.exists() && tempFile.length()>0){
FileInputStream fis = new FileInputStream(tempFile);
byte[] temp = new byte[1024];
int length = fis.read(temp);
String downloadlength = new String(temp, 0, length);
int downloadlengthint = Integer.parseInt(downloadlength);
startposition = downloadlengthint;
}
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(5000);
conn.setRequestMethod("GET");
//重要:请求服务器下载部分的文件指定文件的位置
conn.setRequestProperty("Range", "bytes=" + startposition + "-" + endposition);
InputStream is = conn.getInputStream(); // 已经指定文件的位置,返回相应文件位置的输入流。
RandomAccessFile raf = new RandomAccessFile("ku.exe", "rwd");
//随机写文件的时候从哪个位置开始写
raf.seek(startposition);
int len = 0;
byte[] buffer = new byte[1024];
//File file = new File(threadid + ".txt");
int total = 0;
while( (len = is.read(buffer)) != -1){
// fos = new FileOutputStream(file);
RandomAccessFile file = new RandomAccessFile(threadid + ".txt", "rwd");
raf.write(buffer, 0, len);
total += len;
file.write(String.valueOf(total + startposition + "").getBytes());
file.close();
//fos.write(String.valueOf(total).getBytes());
//fos.flush();
//fos.close();
}
is.close();
raf.close();
System.out.println(startposition + "----》" + endposition + "完成");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//super.run();
}
}