java - FTPClient - 如何使用主动模式

标签 java ftp mode

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

enterLocalPassiveMode() 

最近有人告诉我在 FTP 服务器上不允许使用被动模式,所以我应该让我的应用程序在主动模式下工作。我想这不能通过简单地将方法更改为

enterLocalActiveMode()

我还应该在应用程序中更改什么以确保它在 Activity 模式下工作。

这是一个连接到服务器的代码片段:

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() 之后和调用 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;
}

关于java - FTPClient - 如何使用主动模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21409923/

相关文章:

java - 如何正确启动Jetty

ftp - Windows 上的 FTP 文本编辑器

Java applet 替代技术

c - 从直方图中寻找众数

java - 设置 JTable 内 JTextField 的大小

java - 数据类型为 Double 且值为 '0.0' 的变量未出现在 JSON 响应中

java - 连续运行shell脚本时JVM进入休眠状态

java - 在Hadoop/层叠中从FTP服务器读取数据

python - 如何创建显示均值、中位数和众数的seaborn fiddle 图?