请求头中包含如下 RANGE:bytes=2345-
意思是从2345字节处开始请求数据。这样如果在xx处下载失败,可以通过传递如上请求参数达到续传的目的。
DownloadApp
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Act:
* User:于勇
* Date: 12-12-24
* version:1.0
* To change this template use File | Settings | File Templates.
*/
public class DownloadTask extends Thread{
private DownloadInfo info;
private long start;
private long length;
public DownloadTask(DownloadInfo info){
this.info=info;
}
public DownloadTask(DownloadInfo info,long start,long length){
this.info=info;
this.start=start;
this.length=length;
System.out.println("线程 "+this.getId()+" 开启");
}
@Override
public void run() {
long tmpLen=0;
InputStream input=null;
RandomAccessFile tmpSaveFile=null;
try{
URL url=new URL(info.getdSourcePath());
HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
String sProperty="bytes="+start+"-";
httpURLConnection.setRequestProperty("User-Agent","NetFox");
httpURLConnection.setRequestProperty("RANGE", sProperty);
input = httpURLConnection.getInputStream();
byte[]b=new byte[DownloadInfo.THREAD_READ_LENGTH];
int nRead;
File tmpFile=new File(info.getdSavePath()+File.separator+info.getdSaveName()+".tmp");
tmpSaveFile = new RandomAccessFile(tmpFile,"rw");
if(tmpFile.exists()){
tmpSaveFile.seek(start);
}
while((nRead=input.read(b,0,DownloadInfo.THREAD_READ_LENGTH))>0&&tmpLen<length){
tmpLen+=nRead;
tmpSaveFile.write(b,0,nRead);
}
System.out.println("线程 "+this.getId()+" 下载完成!下载片段["+this.start+"字节至"+(this.start+length)+"字节]");
}catch(Exception e){
System.out.println("线程 "+this.getId()+" 出错");
DownloadTask task=new DownloadTask(info,start+tmpLen,length-tmpLen);
task.start();
DownloadProject.totalThread++;
System.out.println("线程 "+task.getId()+" 开启恢复 从 "+(start+tmpLen)+" ");
}finally{
try {
input.close();
tmpSaveFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DownloadProject.totalThread--;
}
}
public long getLength() {
return length;
}
public void setLength(long length) {
this.length = length;
}
public DownloadInfo getInfo() {
return info;
}
public void setInfo(DownloadInfo info) {
this.info = info;
}
public long getStart() {
return start;
}
public void setStart(long start) {
this.start = start;
}
}
DownloadInfo
/**
* Act:
* User:于勇
* Date: 12-12-24
* version:1.0
* To change this template use File | Settings | File Templates.
*/
public class DownloadInfo {
private String dSourcePath;
private String dSaveName;
private String dSavePath;
public static long THREAD_LENGTH=4096;//1024000
public static int THREAD_READ_LENGTH=2048
;
public DownloadInfo(String sourcePath,String savePath,String saveName){
this.dSourcePath=sourcePath;
this.dSaveName=saveName;
this.dSavePath=savePath;
}
public String getdSourcePath() {
return dSourcePath;
}
public void setdSourcePath(String dSourcePath) {
this.dSourcePath = dSourcePath;
}
public String getdSaveName() {
return dSaveName;
}
public void setdSaveName(String dSaveName) {
this.dSaveName = dSaveName;
}
public String getdSavePath() {
return dSavePath;
}
public void setdSavePath(String dSavePath) {
this.dSavePath = dSavePath;
}
}
DownloadProject
import java.io.File;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Act:
* User:于勇
* Date: 12-12-24
* version:1.0
* To change this template use File | Settings | File Templates.
*/
public class DownloadProject extends Thread{
public static int totalThread=0;
private DownloadInfo info;
public void setInfo(DownloadInfo info) {
this.info = info;
}
@Override
public void run() {
try{
runThreads();
while(totalThread>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
switchFile();
}
}
private void runThreads(){
try{
URL url=new URL(info.getdSourcePath());
HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
String sProperty="bytes="+0+"-";
httpURLConnection.setRequestProperty("RANGE", sProperty);
InputStream input= httpURLConnection.getInputStream();
int length=httpURLConnection.getContentLength();
File tmpFile=new File(info.getdSavePath()+File.separator+info.getdSaveName()+".tmp");
long start=0;
long compLen=0;
if(tmpFile.exists()){
compLen=tmpFile.length();
if(compLen<length){
start+=compLen;
length-=compLen;
}else
return;
}
for(int i=1;i<=length/DownloadInfo.THREAD_LENGTH;i++){
DownloadTask task=null;
task=new DownloadTask(info,start,DownloadInfo.THREAD_LENGTH);
task.start();
totalThread++;
start=start+DownloadInfo.THREAD_LENGTH;
}
if(length%DownloadInfo.THREAD_LENGTH!=0){
DownloadTask task=new DownloadTask(info,start,length%DownloadInfo.THREAD_LENGTH);
task.start();
totalThread++;
start+=length%DownloadInfo.THREAD_LENGTH;
}
input.close();
}catch(Exception e){
e.printStackTrace();
}
}
private void switchFile(){
File oldfile=new File(info.getdSavePath()+File.separator+info.getdSaveName()+".tmp");
File newfile=new File(info.getdSavePath()+File.separator+info.getdSaveName());
oldfile.renameTo(newfile);
}
}
DownloadTask
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Act:
* User:于勇
* Date: 12-12-24
* version:1.0
* To change this template use File | Settings | File Templates.
*/
public class DownloadTask extends Thread{
private DownloadInfo info;
private long start;
private long length;
public DownloadTask(DownloadInfo info){
this.info=info;
}
public DownloadTask(DownloadInfo info,long start,long length){
this.info=info;
this.start=start;
this.length=length;
System.out.println("线程 "+this.getId()+" 开启");
}
@Override
public void run() {
long tmpLen=0;
InputStream input=null;
RandomAccessFile tmpSaveFile=null;
try{
URL url=new URL(info.getdSourcePath());
HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
String sProperty="bytes="+start+"-";
httpURLConnection.setRequestProperty("User-Agent","NetFox");
httpURLConnection.setRequestProperty("RANGE", sProperty);
input = httpURLConnection.getInputStream();
byte[]b=new byte[DownloadInfo.THREAD_READ_LENGTH];
int nRead;
File tmpFile=new File(info.getdSavePath()+File.separator+info.getdSaveName()+".tmp");
tmpSaveFile = new RandomAccessFile(tmpFile,"rw");
if(tmpFile.exists()){
tmpSaveFile.seek(start);
}
while((nRead=input.read(b,0,DownloadInfo.THREAD_READ_LENGTH))>0&&tmpLen<length){
tmpLen+=nRead;
tmpSaveFile.write(b,0,nRead);
}
System.out.println("线程 "+this.getId()+" 下载完成!下载片段["+this.start+"字节至"+(this.start+length)+"字节]");
}catch(Exception e){
System.out.println("线程 "+this.getId()+" 出错");
DownloadTask task=new DownloadTask(info,start+tmpLen,length-tmpLen);
task.start();
DownloadProject.totalThread++;
System.out.println("线程 "+task.getId()+" 开启恢复 从 "+(start+tmpLen)+" ");
}finally{
try {
input.close();
tmpSaveFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DownloadProject.totalThread--;
}
}
public long getLength() {
return length;
}
public void setLength(long length) {
this.length = length;
}
public DownloadInfo getInfo() {
return info;
}
public void setInfo(DownloadInfo info) {
this.info = info;
}
public long getStart() {
return start;
}
public void setStart(long start) {
this.start = start;
}
}
分享到:
相关推荐
java 多线程断点续传 java 多线程断点续传 java 多线程断点续传
java多线程断点续传[借鉴].pdf
该程序实现了java的多线程下载,显示下载进度条
java多线程断点续传.pdf
刚刚整理的java断点续传,多线程处理,不知道怎么把多线程去掉
java实现FTP多线程断点续传
1. 把每个下载文件切成若干个块(Block),然后得到一个位图,用来标记每个块的下载情况,并保存到文件里,用于实现断点续传。 2. HTTP Header里增加Range,如果服务器返回Cotent-Range 说明服务器支持文件定位,可以...
这是我java的课程设计。本程序实现类似迅雷的多线程断点续传下载功能,并且有简单的界面,可以显示下载中各个线程的下载状况。 用Eclipse编译一下就能运行了。希望和大家共同学习!
java实现FTP多线程断点续传,上传下载! - java学习与交流 - j2ee学习网 - j2ee学习网 (2012年5月21日) 用 Java 实现断点续传 (HTTP) (2012年5月21日) Java写的支持断点续传的FTP--crybaby的博客 (2012年5月21日) ...
java FTP多线程 批量 断点续传,使用 1.建一个支持多线程FTP服务端; 如:xxftp有Windows和Linux版本; 2.建一个项目导入文件测试;
java相关的 ftp知识 如:ftp服务器、ftp主被动通讯模式、断点续传、单个文件多线程下载
最近研究了一下socket套接字 实现java多线程 断点续传文件 在网上查找了很多例子 然后学习了大家的方法 最后利用网上的例子 自己整合了一份多线程 断点续传文件的代码 并且能实现客户端发送完毕之后 接收服务器端的...
这是java实现多线程断点续传下载功能的源代码,对于学习java网络编程、多线程等基础知识的同学有一定的参考作用,希望能够这个资源能够对你们有帮助
java中http断点续传的实现原理,介绍了主要方法和简单的实现
多线程 断点续传 java 下载 java多线程实例
应用多线程MultiThread断点续传工具类,经典小例子。可运行。
这个是java开发的多线程断点续传下载demo,内有详细注释,是练习android多线程断点续传下载时写的测试demo,下面的android开发的多线程断点续传下载demo是基于这个移植过去的,使用java开发的可以参考这个。
自己为了课程设计写的程序,基于java的多线程断点续传程序
socket做的支持多线程断点上传or断点续传Java源码
Java多线程与线程安全实践-基于Http协议的断点续传 Java多线程与线程安全实践-基于Http协议的断点续传 Java多线程与线程安全实践-基于Http协议的断点续传 Java多线程与线程安全实践-基于Http协议的断点续传 Java多...