断点续传


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();
}
}

}

绿色字为续传代码.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值