新闻资讯

FTP连接超时和判断连接状态如何解决?ftp连接好用的方法推荐

2019-10-14

我想写一个支持断点续传的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;
}

ftp连接有没有好用的方法?

那么说到利用花生壳搭建ftp服务,要从下载、安装、登录动态域名解析服务开始,保证域名解析准确。使用Serv—U来搭建ftp服务,进入SERV-U的本地服务器,点击“新建域”,命名为“MYftp”,然后点击下一步,其他都使用系统默认的设置点下一步直至完成。域已经建立好,这个时候,会提示是否需要给域建立用户,选择“是”,会提示是否需要根据向导来新建用户名。根据提示添加一个用户名为贝锐的账户这时就安装并创建好ftp服务。测试内网是否可以访问ftp站点。在获取本机内网IP后,获取本机内网IP方法如下:通过点击电脑开始——运行——CMD,然后回车,在出现的DOS界面里面输入ipconfig/all ,通过查看IP Address获得本机内网IP,打开IE浏览器进行测试,。现在内网已经可以访问了,内网ftp搭建工作完成。

但是在这个过程中,需要注意的是如果有路由器上网,那么需要从外网访问,需要进行端口映射操作。第一种办法是:点击转发规则——虚拟服务器,做好端口映射。另一种方法是:点击转发规则——DMZ主机,DMZ主机的IP地址填写服务器IP地址,勾选启用,点击保存即可。这种方法一般不建议采用,因为采用DMZ主机后,本机的所有端口将完全暴露在广域网,请慎用。那么以上就是关于使用花生壳来实现ftp服务的内容,如有疑问,还可做更多的了解。