import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
public class JavaApplication15 { public static void main(String[] args) throws IOException { String Netpath = "http://dl_dir2.qq.com/invc/xfspeed/qdesk/versetup/QDeskSetup_25_1277.exe";
String computerPath = "C:\\Users\\sj\\Desktop\\xx.exe";
int threadLength = 10;
int lengthAll = 0;//文件的总长度 try {
URL url = new URL(Netpath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
lengthAll = connection.getContentLength();
RandomAccessFile raf = new RandomAccessFile(computerPath, "rw");
raf.setLength(lengthAll);
raf.close();
} catch (MalformedURLException ex) {
Logger.getLogger(CODownloadThread.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) {
Logger.getLogger(CODownloadThread.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("开始下载额");
//计算每条线程的下载长度
int temp = lengthAll / threadLength;
for (int i = 0; i < threadLength; i++) {
int startPosition = temp * i;
RandomAccessFile raf = new RandomAccessFile(computerPath, "rw");
raf.seek(startPosition);
new CODownloadThread(Netpath, startPosition, i, temp, raf).start();
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.RandomAccess;
import java.util.logging.Level;
import java.util.logging.Logger;/**
*
* @author sj
*/
public class CODownloadThread extends Thread { private String urlPath; //网络上资源的路径
private int startPosition;//每条线程开始的位置
private int id; //线程的id
private int lengh; //每条线程要下载的长度
private RandomAccessFile RAfile; //用于断点存放的随机存储类 public CODownloadThread(String urlPath, int startPosition, int id, int lengh, RandomAccessFile RAfile) { this.urlPath = urlPath;
this.startPosition = startPosition;
this.id = id;
this.lengh = lengh;
this.RAfile = RAfile;
} @Override
public void run() {
try {
URL url = new URL(urlPath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setRequestProperty("Range", "bytes=" + startPosition + "-");
InputStream in = connection.getInputStream();
byte[] byt = new byte[1024];
System.out.println("线程" + id + "开始下载");
int a = 0;
while ((a = in.read(byt)) != -1) {
RAfile.write(byt, 0, a);
}
RAfile.close();
in.close();
System.out.println("线程" + id + "已经完成了"); } catch (MalformedURLException ex) {
Logger.getLogger(CODownloadThread.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(CODownloadThread.class.getName()).log(Level.SEVERE, null, ex);
} }
}问题1,怎么计算当前下载了多少了,是所有线程加在一起的大小
问题2,怎么暂停这些并记录位置,下次怎么开始
谢谢高手们
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
public class JavaApplication15 { public static void main(String[] args) throws IOException { String Netpath = "http://dl_dir2.qq.com/invc/xfspeed/qdesk/versetup/QDeskSetup_25_1277.exe";
String computerPath = "C:\\Users\\sj\\Desktop\\xx.exe";
int threadLength = 10;
int lengthAll = 0;//文件的总长度 try {
URL url = new URL(Netpath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
lengthAll = connection.getContentLength();
RandomAccessFile raf = new RandomAccessFile(computerPath, "rw");
raf.setLength(lengthAll);
raf.close();
} catch (MalformedURLException ex) {
Logger.getLogger(CODownloadThread.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) {
Logger.getLogger(CODownloadThread.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("开始下载额");
//计算每条线程的下载长度
int temp = lengthAll / threadLength;
for (int i = 0; i < threadLength; i++) {
int startPosition = temp * i;
RandomAccessFile raf = new RandomAccessFile(computerPath, "rw");
raf.seek(startPosition);
new CODownloadThread(Netpath, startPosition, i, temp, raf).start();
}
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.RandomAccess;
import java.util.logging.Level;
import java.util.logging.Logger;/**
*
* @author sj
*/
public class CODownloadThread extends Thread { private String urlPath; //网络上资源的路径
private int startPosition;//每条线程开始的位置
private int id; //线程的id
private int lengh; //每条线程要下载的长度
private RandomAccessFile RAfile; //用于断点存放的随机存储类 public CODownloadThread(String urlPath, int startPosition, int id, int lengh, RandomAccessFile RAfile) { this.urlPath = urlPath;
this.startPosition = startPosition;
this.id = id;
this.lengh = lengh;
this.RAfile = RAfile;
} @Override
public void run() {
try {
URL url = new URL(urlPath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setRequestProperty("Range", "bytes=" + startPosition + "-");
InputStream in = connection.getInputStream();
byte[] byt = new byte[1024];
System.out.println("线程" + id + "开始下载");
int a = 0;
while ((a = in.read(byt)) != -1) {
RAfile.write(byt, 0, a);
}
RAfile.close();
in.close();
System.out.println("线程" + id + "已经完成了"); } catch (MalformedURLException ex) {
Logger.getLogger(CODownloadThread.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(CODownloadThread.class.getName()).log(Level.SEVERE, null, ex);
} }
}问题1,怎么计算当前下载了多少了,是所有线程加在一起的大小
问题2,怎么暂停这些并记录位置,下次怎么开始
谢谢高手们
其实这个currentDownLength就是你在线程里write完累加的。
问题2:这个根据迅雷的设计方案,我们认真观察下以前的迅雷在下载文件的时候,每个下载未完成的文件都会有个该文件名+.td的文件产生这个我们也可以根据迅雷的设计思路来弄个。我们在每个下载文件时也产生对应的记录文件
这个文件用来记录当前上次有几个线程在下载。每个线程都分别下载了多少。也就是问题1中提到的currentDownLength我们可以记录到这里面去,下次再下载这个文件时我们就可以先去这个记录文件查阅下,下载历史。然后再进行下载。