java实现断点续传


/**
* @{#} 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);
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值