Java FTP客户端-如何使用活动模式
我制作了一个小应用程序,可以将文件上传到FTP服务器。问题是,我使用了被动模式的方法Java FTP客户端-如何使用活动模式,java,ftp,mode,Java,Ftp,Mode,我制作了一个小应用程序,可以将文件上传到FTP服务器。问题是,我使用了被动模式的方法 enterLocalPassiveMode() 最近我被告知FTP服务器上不允许使用被动模式,所以我应该让我的应用程序在主动模式下工作。我想,仅仅将方法更改为 enterLocalActiveMode() 我还应该在应用程序中更改什么以确保它在活动模式下工作 以下是与服务器建立连接的代码片段: public void connect() throws FTPException { try
enterLocalPassiveMode()
最近我被告知FTP服务器上不允许使用被动模式,所以我应该让我的应用程序在主动模式下工作。我想,仅仅将方法更改为
enterLocalActiveMode()
我还应该在应用程序中更改什么以确保它在活动模式下工作
以下是与服务器建立连接的代码片段:
public void connect() throws FTPException {
try {
ftpClient.connect(server, port);
replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
printText("FTP server refused connection.");
throw new FTPException("FTP server refused connection.");
}
boolean logged = ftpClient.login(user, pass);
if (!logged) {
ftpClient.disconnect();
printText("Could not login to the server.");
throw new FTPException("Could not login to the server.");
}
ftpClient.enterLocalPassiveMode();
} catch (IOException ex) {
printText("I/O errortest: " + ex.getMessage());
throw new FTPException("I/O error: " + ex.getMessage());
}
}
关于我必须改变什么的一些指导?
不确定这是否是您正在使用的FTPClient,但看起来enterLocalActiveMode确实存在。
FTPClient
文档说明
public boolean enterRemoteActiveMode(InetAddress主机,int端口)
抛出IOException
将当前数据连接模式设置为活动\远程\数据\连接。此方法仅用于服务器到服务器的数据传输。此方法向服务器发出一个PORT命令,指示另一台服务器和它应连接以进行数据传输的端口。您必须在每次服务器到服务器传输尝试之前调用此方法。FTPClient将不会自动继续发出端口命令。如果希望返回到正常数据连接模式,还必须记住调用enterLocalActiveMode()
希望这有帮助 这是一个很老的问题,但我在试图自己解决这个问题时偶然发现了它 在调用
connect()
之后和调用login()
之前,必须调用enterLocalPassiveMode()
请参阅下面的示例,该示例在本地被动模式下初始化FTPClient
,列出给定目录的文件,然后关闭连接
private static FTPClient client;
public static void main(String [] args) {
//initialise the client
initPassiveClient();
//do stuff
FTPFile [] files = listFiles("./");
if( files != null ) {
logger.info("Listing Files:");
for( FTPFile f : files) {
logger.info(f.getName());
}
}
//close the client
close();
}
/**
* getPassiveClient retrive a FTPClient object that's set to local passive mode
*
* @return FTPClient
*/
public static FTPClient initPassiveClient() {
if( client == null ) {
logger.info("Getting passive FTP client");
client = new FTPClient();
try {
client.connect(server);
// After connection attempt, you should check the reply code to verify
// success.
int reply = client.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
logger.error("FTP server refused connection.");
System.exit(0);
}
//after connecting to the server set the local passive mode
client.enterLocalPassiveMode();
//send username and password to login to the server
if( !client.login(user, pass) ) {
logger.error("Could not login to FTP Server");
System.exit(0);
}
} catch (SocketException e) {
String message = "Could not form socket";
logger.error(message+"\n", e);
System.exit(0);
} catch (IOException e) {
String message = "Could not connect";
logger.error(message+"\n", e);
System.exit(0);
}
}
return client;
}
public static void close() {
if( client == null ) {
logger.error("Nothing to close, the FTPClient wasn't initialized");
return;
}
//be polite and logout & close the connection before the application finishes
try {
client.logout();
client.disconnect();
} catch (IOException e) {
String message = "Could not logout";
logger.error(message+"\n", e);
}
}
/**
* listFiles uses the FTPClient to retrieve files in the specified directory
*
* @return array of FTPFile objects
*/
private static FTPFile[] listFiles(String dir) {
if( client == null ) {
logger.error("First initialize the FTPClient by calling 'initFTPPassiveClient()'");
return null;
}
try {
logger.debug("Getting file listing for current director");
FTPFile[] files = client.listFiles(dir);
return files;
} catch (IOException e) {
String message = "";
logger.error(message+"\n", e);
}
return null;
}
传输是否在两个远程服务器之间进行?您是否检查了此
FTPClient.enterRemoteActiveMode(java.net.InetAddress,int)
?是的,计算机(客户端和服务器)通过Internet远程连接-它们不在同一网络中。然后尝试此操作,FTPClient.enterRemoteActiveMode(远程IP,端口)
在连接之前。在每次IOException的情况下,为什么还要麻烦重新调用FTPEException?提前感谢是的,这是我正在使用的FTPClient,但是这个方法会在活动模式下启动连接而不在代码中执行任何其他操作吗?我这样问是因为FTP服务器不可用,所以我现在无法测试连接。非常感谢您的回答-非常感谢!。因此,我不应该使用enterLocalPassiveMode()
而是应该使用enterRemoteActiveMode(InetAddress主机,int端口)
。host
参数我相信是我上传到的服务器的地址。但是港口呢?应该是端口21还是端口20?是的,绝对是!默认情况下,它是端口21。但是,请检查您的FTP服务器是否配置为在任何其他端口中运行。我迫不及待地想打开FTP服务器,所以我很快在mu Linux机器上创建了一个vsftpd
服务器来进行尝试。嗯,文件上传不成功。它只是在服务器上创建的,是空的。我使用该方法的方式如下:booleanFoo=enterRemoteActiveMode(InetAddress.getByName(“XX.XX.XX.XX”),21)
。有什么想法吗?P.S.我用pasv_enable=NO
启用了vsftpd的活动模式。因此,我知道该文件已创建,但为空-0字节
。然后,检查vsftpd中是否没有写访问权限或任何禁用写操作的配置