我想写一个支持断点续传的FTP,之前用apache commons-net-1.4.1,
listFiles()这个方法怎么都搞不定,就导入了FtpBean包。
我需要写一个判断FTP连接状态的方法boolean isConnected(),当他返回true的
时候表示FTP处于连接状态,并且可以上传,返回false会重新连接FTP。
谁帮我看一下这段程序,现在连接上FTP后一段时间没有动作会有连接超时的问题
ftp.FtpException: 421 Connection timed out.
还有no transfer time,closing control connection.
怎么判断超时了和连接断开了
谢谢!import ftp.FtpBean;
import ftp.FtpException;
import ftp.FtpListResult;
import ftp.SocketOpener;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;/**
*
* @author happykiki
*/
public class ConnectServer { public ConnectServer() {this.ftp = new FtpBean();
this.remoteFileSize = 0;
this.timeout = 5000;
}/**
* This method connect to FTP Server and login.
* @param server Name of server. 
* @param username user name for login.
* @param password password for login.
*/ 
public FtpBean connect (String server, String username, String password) {this.server = server;
try {this.ftp.setSocketTimeout(this.timeout); 
// Create socket, get output stream
socket = new SocketOpener (server, 21).makeSocket(timeout); 
socket.setKeepAlive(true);
out = new PrintWriter(socket.getOutputStream(), true);
this.ftp.ftpConnect(server, username, password); 
java.lang.System.out.println(this.ftp.getReply());
java.lang.System.out.println("************** File in the FTP server *****************");
java.lang.System.out.println(this.ftp.getDirectoryContentAsString());
java.lang.System.out.println("*******************************************************"); 
} catch (IOException ex) {
System.out.println(ex);
} catch (FtpException ex) {
System.out.println(ex);
}
return this.ftp;
} /** 
* This method checks whether the file exist on ftp.
* @param localFile Name of local file, can be in full path.
* @param aFTP Current connection of ftp server.
* @return exist.
*/ 
public boolean existOnFtp (File localFile) {boolean exist = false;
try {FtpListResult ftplist = new FtpListResult(); 
ftplist = this.ftp.getDirectoryContent();
while(ftplist.next()) {if( localFile.getName().equals(ftplist.getName()) ) {this.remoteFileSize = ftplist.getSize();
exist = true;
break;
}

} catch (Exception ex) {ex.printStackTrace();
}
return exist;
}/** 
* This method checks whether the address is a ftp server.
*/ 
public boolean isServer() {String systemType = "";
try {//Get the type of operating system of the server. 
//Return null if it is not currently connected to any ftp server.
systemType = this.ftp.getSystemType();
} catch (IOException ex) {System.out.println(ex);
} catch (FtpException ex) {System.out.println(ex);
}if ( systemType == null ) {return false;
} else {return true;
}
}/** 
* This method checks whether the file complete uploaded on ftp.
* @param localFile Name of local file, can be in full path.
* @return uploaded.
*/
public boolean uploaded (File localfile) {boolean uploaded = false;
if (localfile.length() == this.remoteFileSize) {uploaded = true;
}
return uploaded;
}/**
* This method decide whether FTP connected . 
*/
public boolean isConnected() {if (out != null && socket.isConnected()) {return true;
} else {return false;
}} /**
* This method close FTP connection.
*/
public void closeConnect() {if ( isConnected() ) {try {this.ftp.close();
System.out.println("disconnect success");
} catch (Exception ex) {System.out.println(ex);

}
}private FtpBean ftp;
private long remoteFileSize;
private Socket socket;
private int timeout; 
public PrintWriter out; // Output for FTP connection
private String server;
}