前几天看到有某位网友写了用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;
publicclass TestProtocolFtp {
private FTPClientFTP;
private Stringhost;
privateintport;
private Stringuser;
private Stringpwd;
private Stringtaskdate;
privatebooleanforceexit;
privatestatic Loggerlog =LogMgr.getInstance().getSystemLogger();
public String toString() {
returnhost +":" +port +"@" +user +"/" +pwd;
}
publicboolean Login(String strHost,int nPort, String strUser,
StringstrPwd) {
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("FTPserver refused connection.");
returnfalse;
}
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("FTPlogin", se);
}catch (Exception e) {
log.error("FTPlogin", e);
}
return bOK;
}
publicvoid Close() {
try {
if (FTP !=null) {
FTP.logout();
FTP.disconnect();
}
}catch (Exception e) {
}
}
privateboolean ftpValidate() {
if ((FTP !=null)&& (FTP.isConnected()))
returntrue;
else
returnfalse;
}
publicboolean ReLogin() {
int i = 1;
boolean ret =false;
if (ftpValidate()) {
returntrue;
}
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 MonitorThreadextends Thread {
FTPClientftpClient =null;
intnSeconds = 0;
ThreadftpThread =null;
public MonitorThread(int nSeconds, FTPClient ftp, Thread t) {
ftpClient = ftp;
this.nSeconds = nSeconds;
this.ftpThread = t;
}
publicvoid 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("Interruptftp error", e);
}
}
}
}
privatelongcontentLength = 0;
/**从服务器下载指定的文件到本地,返回本地文件名称,如果返回为