public class DownLoad {
public static String path = "http://192.168.0.101:8080/11.mp4";
public static void main(String[] args) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(3000);
conn.setRequestProperty("User-Agent",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
if (conn.getResponseCode() == 200) {
int len = conn.getContentLength();
RandomAccessFile file = new RandomAccessFile(getFileName(path), "rwd");
file.setLength(len);
int threadnumber = 3;
int blocksize = len / threadnumber;
for (int i = 0; i < threadnumber; i++) {
int startfile = i * blocksize;
int endfile = (i + 1) * blocksize;
if (i == (threadnumber - 1)) {
endfile = len;
}
DownLoadTask task = new DownLoadTask(i, path, startfile, endfile);
task.start();
}
}
}
public static String getFileName(String path) {
int start = path.lastIndexOf("/") + 1;
return path.substring(start, path.length());
}
}
class DownLoadTask extends Thread {
int threadid;
String filepath;
int startfile;
int endfile;
public static String path = "http://192.168.0.101:8080/11.mp4";
public static String getFileName(String path) {
int start = path.lastIndexOf("/") + 1;
return path.substring(start, path.length());
}
public DownLoadTask(int threadid, String filepath, int startfile, int endfile) {
super();
this.threadid = threadid;
this.filepath = filepath;
this.startfile = startfile;
this.endfile = endfile;
}
public void run() {
try {
// 1记录下载位置的文件
File positionfile = new File(threadid + ".txt");
URL url = new URL(filepath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (positionfile.exists()) {
// 2.从记录的文件中读取位置并设置新的开始
FileInputStream fis = new FileInputStream(positionfile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
fis.close();
byte[] b = bos.toByteArray();
// 以上代码把输入流转化成byte[]
int newposition = Integer.parseInt(new String(b));
if (newposition > startfile) {
startfile = newposition;
}
}
conn.setRequestProperty("Range", "bytes=" + startfile + "-" + endfile);
conn.setRequestMethod("GET");
conn.setConnectTimeout(3000);
conn.setRequestProperty("User-Agent",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)");
InputStream is = conn.getInputStream();
RandomAccessFile file = new RandomAccessFile(getFileName(path), "rwd");
file.seek(startfile);
byte[] buffer = new byte[1024];
int len = 0;
int currentposition = startfile;
while ((len = is.read(buffer)) != -1) {
file.write(buffer, 0, len);
currentposition += len;
String ss = currentposition + "";
FileOutputStream fos = new FileOutputStream(positionfile);
fos.write(ss.getBytes());
fos.flush();
fos.close();
}
file.close();
System.out.println("线程" + threadid + "下载完成");
if (positionfile.exists()) {
positionfile.delete();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
绿色字为续传代码.