双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

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

happykiki happykiki 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我想写一个支持断点续传的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;
}
...全文

相关推荐

java ftp连接池

在网上找了好久没有现成的ftp连接池jar包,自己花了一些时间实现了一个简单的连接池,用了一段时间稳定性还可以。

wd501771382

ftp可登录但是无法下载,上传问题解决

构建了一个ftp服务器,通过命令ftp可登录,登录后,无法下载,上传,ls也无法执行。 通过 passive 开启和关闭 PASV模式,再ls 也是同样的问题, # 主动模式 $ pftp -v -A -d 211.159.185.xxx Connected to 211.159.185.198 (211.159.185.xxx). 220 (vsFTPd 3.0.2) Name (211.159.185.xxx:liangzh): lebaFtpUser ---> USER lebaFtpUser 331 Please specify the password. Password: ---> PASS XXXX 230 Login successful. ---> SYST 215 UNIX Type: L8 Remote system type is UNIX. Using binary mode to transfer files. ftp> ftp> ls ftp: setsockopt (ignored): Permission denied ---> PORT 192,168,10,144,210,113 200 PORT command successful. Consider using PASV. ---> LIST 425 Failed to establish connection. # 被动模式 $ ftp -v -p -d 211.159.185.198 Connected to 211.159.185.198 (211.159.185.198). 220 (vsFTPd 3.0.2) Name (211.159.185.198:liangzh): lebaFtpUser ---> USER lebaFtpUser 331 Please specify the password. Password: ---> PASS XXXX 230 Login successful. ---> SYST 215 UNIX Type: L8 Remote system type is UNIX. Using binary mode to transfer files. ftp> ls ftp: setsockopt (ignored): Permission denied ---> PASV 227 Entering Passive Mode (211,159,185,198,134,173). ftp: connect: Connection timed out 解决这个问题: 1. setsebool -P ftpd_disable_trans 1 2. service vsftpd restart

csdn1054116023

关于C#连接FTP时路径问题的解决方法

最近在工作中遇到一个需求,需要利用C#连接FTP,在连接过程中遇到一个问题,所以下面这篇文章主要给大家介绍了关于C#连接FTP时路径问题的解决方法,需要的朋友可以参考借鉴,下面来一起看看吧。

weixin_38699551

springboot集成ftp连接池工具

springboot集成ftp连接池工具,很好的解决了并发大用户上传附件慢的问题

lh_life

java判断ftp目录是否存在的方法

主要为大家详细介绍了java判断ftp目录是否存在的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

weixin_38682790
评论 5
收藏 已收藏
分享
返回顶部
happykiki
Java SE 62,621 成员 307,251 内容