/**
* @{#} 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);
}
}
}
分享到:
相关推荐
用Java实现断点续传.txt 用Java实现断点续传.txt 用Java实现断点续传.txt
包括一个客户端java文件,可以用来上传文件,但是要修改文件的地址;和一个服务端的完整JavaWeb项目,需要自己导入开发工具中调试
内容:(一)断点续传的原理 (二)Java实现断点续传的关键几点 (三)断点续传内核的实现
用apache的FTP实现断点续传 - janestone的专栏 - 博客频道 - CSDN.NET (2012年5月21日) java实现FTP多线程断点续传,上传下载! - java学习与交流 - j2ee学习网 - j2ee学习网 (2012年5月21日) 用 Java 实现断点续传 ...
内容:(一)断点续传的原理 (二)Java实现断点续传的关键几点 (三)断点续传内核的实现
java实现断点续传 private void setHeader(URLConnection con) { con .setRequestProperty( "User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092510 Ubuntu/8.04 ...
用 Java 实现断点续传 (HTTP),内容有: 断点续传的原理, Java 实现断点续传的关键几点, 断点续传内核的实现等。
本文介绍断点续传的基本原理, 及java的实现方式。
1.断点续传的原理 2.用什么方法实现提交range bytes 3.保存文件采用什么方法
java实现FTP多线程断点续传
java实现ftp断点续传
Java 实现断点续传 (HTTP)的Demo
java实现断点续传的小例子(源代码),偶尔拿来当迅雷用。
java FTP多线程 批量 断点续传,使用 1.建一个支持多线程FTP服务端; 如:xxftp有Windows和Linux版本; 2.建一个项目导入文件测试;