java实现断点续传
2009-09-16 00:00:00 来源:WEB开发网/**
* @{#} MainThreadDownFile.java
* Create on 2009-6-17 下午11:00:14
*
* @author 何明
* @version 1.0
*
* Copyright (c) 2007 by GTT.
*/
package com.down;
import java.io.File;
import com.common.Common;
import com.common.ExceptionManager;
public class MainThreadDownFile{
private String url;
private String localsavePath;
private String localsaveName;
private int threadNum;
private static String tempStoreDirectory;//临时文件路径
private long eachThreadSzie;//平均每个线程获取文件的大小
boolean isDownStop=false;
private int entid;
public MainThreadDownFile(int entid,String url,String savePath,String saveName,int threadNum) {
this.entid=entid;
this.url=url;
this.localsavePath=savePath;
this.localsaveName=saveName;
this.threadNum=threadNum;
if(!createDirectory(localsavePath)){ //创建存放附件的地址
Common.WriteLog("创建文件失败!");
return;
}
}
/**创建存放临时文件的临时文件夹*/
private String getTempStoreDirectory(String tempDirctoryName){
String tempDirctory = localsavePath +"\\"+ tempDirctoryName;
String tNum = "";
do{
tempDirctory = tempDirctory + tNum;
File file = new File(tempDirctory);
if(file.exists()) tNum = "0";
else return tempDirctory;
} while(true);
}
/**判断是否有存放附件文件夹,如果没有就创建*/
private boolean createDirectory(String localFileAddress){
try{
File file = new File(localFileAddress);
if(!file.exists())
file.mkdir();
}catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
public void getEachThreadFileSize(long size){
eachThreadSzie=size/threadNum;
}
public boolean checkExistTPFile(){
File file = new File(tempStoreDirectory);
String fileList[] = file.list();
if(fileList.length > 0){
for(int i = 0; i < fileList.length; i++)
if(fileList[i].indexOf(".tp") > 0)
return true;
}
return false;
}
public void init(NetManager nm) throws ExceptionManager{
try{
if(nm.getCode()==200){
String tempDirctoryName="tmp";
DownInfoWriteXml diwx=new DownInfoWriteXml(localsavePath);
String localSaveAddress=diwx.downFileExist(url); //获取临时附件地址
if(localSaveAddress!=null){
this.tempStoreDirectory=localSaveAddress;
}else{
synchronized(this){
tempStoreDirectory=getTempStoreDirectory(tempDirctoryName);
createDirectory(tempStoreDirectory);
diwx.addOneDownRecord(url, tempStoreDirectory);
diwx.saveChange();
}
}
long filesize=nm.getFileSize();
String filename=Common.getSuffixName(nm.getUrlFileName(),"/");
String tempThreadfileName=tempStoreDirectory + "\\" + filename + ".part";
getEachThreadFileSize(filesize);
SingleThreadDownFile[] stdf=new SingleThreadDownFile[threadNum];
long startPos=0L,endPos=0L;
for(int i=1;i<=threadNum;i++){
if(i>1) startPos+=eachThreadSzie;
endPos=startPos+eachThreadSzie;
DownInfo di=new DownInfo(i,url,startPos,endPos);
stdf[i-1]=new SingleThreadDownFile(tempThreadfileName+i);
stdf[i-1].setDi(di);
stdf[i-1].start();
}
String str[]=new String[threadNum];
boolean isStop=true;
DownState ds=new DownState(filesize);
while(isStop){
int n=0;
long fsize=0L;
long totalsize=0L;
for(int i=1;i<=threadNum;i++){
if(stdf[i-1].isAlive())
fsize += stdf[i-1].downPace;
else n++;
long tsize=new Long(stdf[i-1].downSize);
if(tsize==0){
totalsize += Common.FileSize(tempThreadfileName+i);
}else{
totalsize += tsize;
}
}
if(n==threadNum) isStop=false;
ds.updateDownSize(fsize,totalsize);
ds.init(isStop);
}
if(!checkExistTPFile()&&!isStop){
FileCombination fc=new FileCombination(url,tempStoreDirectory,localsavePath,localsaveName);
fc.init();
isDownStop=true;
}
}else{
Common.WriteLog(nm.getHttpMessage(nm.getCode()));
}
}catch(Exception e){
throw new ExceptionManager(entid,"出现异常",e);
}
}
}
更多精彩
赞助商链接