作者 : 李志涛
邮箱地址 :lizhitao67116961@163.com
深圳优网科技有限公司
前几天看到有某位网友写了用http进行断点下载文件,但是网上没有看到ftp断点下载的文章或代码,现在本人写了一下。经过测试没有问题。请大家多多指教。如有疏漏的地方请各位技术友人指出。本人不胜感激。
package test.download;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.SocketException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
import util.LogMgr;
public class TestProtocolFtp {
private FTPClient FTP ;
private String host ;
private int port ;
private String user ;
private String pwd ;
private String taskdate ;
private boolean forceexit ;
private static Logger log = LogMgr.getInstance ().getSystemLogger();
public String toString() {
return host + ":" + port + "@" + user + "/" + pwd ;
}
public boolean Login(String strHost, int nPort, String strUser,
String strPwd) {
host = strHost;
port = nPort;
user = strUser;
pwd = strPwd;
boolean bOK = false ;
try {
if ( FTP == null ) {
FTP = new FTPClient();
} else {
try {
FTP .disconnect();
} catch (Exception e) {
}
}
log .debug( " 登录用户 :" + strUser + " 密码 :" + strPwd);
FTP .setDefaultTimeout(3600 * 1000);
FTP .setConnectTimeout(3600 * 1000);
FTP .connect(strHost, nPort);
int reply = FTP .getReplyCode();
if (!FTPReply.isPositiveCompletion (reply)) {
FTP .disconnect();
log .error( "FTP server refused connection." );
return false ;
}
bOK = FTP .login(strUser, strPwd);
if (bOK) {
FTP .enterLocalPassiveMode();
FTP .setFileType(FTPClient. BINARY_FILE_TYPE ); // 设置为二进制传输模式
FTP .setDataTimeout(3600 * 1000);
} else {
log
.error( "FTP server Login Failure Code:"
+ FTP .getReplyCode());
}
} catch (SocketException se) {
log .error( "FTP login" , se);
} catch (Exception e) {
log .error( "FTP login" , e);
}
return bOK;
}
public void Close() {
try {
if ( FTP != null ) {
FTP .logout();
FTP .disconnect();
}
} catch (Exception e) {
}
}
private boolean ftpValidate() {
if (( FTP != null ) && ( FTP .isConnected()))
return true ;
else
return false ;
}
public boolean ReLogin() {
int i = 1;
boolean ret = false ;
if (ftpValidate()) {
return true ;
}
while (!ftpValidate()) {
if (i > 3) {
return ret;
}
log .debug( ": 第 " + i + " 次 ReLogin 登陆 ftp:" + host + "," + user );
try {
Thread.sleep (1000 * i * 30);
} catch (Exception e) {
log .error( ": ftp relogin failed. " , e);
}
ret = Login( host , port , user , pwd );
if (ret)
break ;
i++;
}
return ret;
}
class MonitorThread extends Thread {
FTPClient ftpClient = null ;
int nSeconds = 0;
Thread ftpThread = null ;
public MonitorThread( int nSeconds, FTPClient ftp, Thread t) {
ftpClient = ftp;
this . nSeconds = nSeconds;
this . ftpThread = t;
}
public void run() {
if ( nSeconds > 0) {
try {
log .debug( ": sleep 开始 " );
Thread.sleep ((( long ) nSeconds ) * 1000);
log .debug( ": sleep 结束 " );
} catch (InterruptedException e) {
log .debug( ": Monitor thread interrupted by ftp thread" );
return ;
}
try {
log .debug( " ftp timeout for " + nSeconds
+ " seconds, interrupt ftp thread" );
ftpThread .interrupt();
} catch (Exception e) {
log .error( "Interrupt ftp error" , e);
}
}
}
}
private long contentLength = 0;
/** 从服务器下载指定的文件到本地,返回本地文件名称,如果返回为