java ftpclient 主动模式_java-FTPClient-如何使用活动模式

本文档讲述了如何在Java FTP应用程序中从被动模式切换到主动模式,以解决服务器限制问题。关键步骤包括在connect()方法中重新安排enterLocalPassiveMode()调用,并确保在登录前后正确设置。作者提供了示例代码以帮助开发者顺利迁移至主动模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我做了一个小应用程序,应该将文件上传到FTP服务器.问题是我在方法中使用了被动模式

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());

}

}

对我必须更改的一些指导?

解决方法:

这很老,但是我偶然发现了它,试图自己解决问题.

您必须在调用connect()之后且在loging()之前调用enterLocalPassiveMode().

请参阅下面的示例,该示例以本地被动模式初始化FTPClient,列出给定目录的文件,然后关闭连接.

private static FTPClient client;

public static void main(String [] args) {

//initialzie 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;

}

标签:mode,ftp,java

来源: https://codeday.me/bug/20191122/2057743.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值