如何解决FTP连接超时和判断连接状态

happykiki 2007-09-07 06:07:59
我想写一个支持断点续传的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;
}
...全文
2814 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
老紫竹 2012-08-15
  • 打赏
  • 举报
回复
接上FTP后一段时间没有动作会有连接超时的问题

FTP服务器的设置,你超过时间没有数据来往,就断开连接了。 和你客户端无关!
happykiki 2007-09-10
  • 打赏
  • 举报
回复
再问!!!高手呢
happykiki 2007-09-10
  • 打赏
  • 举报
回复
救命啊!这么久了都没人回!
happykiki 2007-09-07
  • 打赏
  • 举报
回复
有人知道吗
急!!!!!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧