Java FTPClient - 如何使用主动模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21409923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:33:37  来源:igfitidea点击:

FTPClient - how to use active mode

javaftpmode

提问by plamenbv

I made a small application that should upload files to an FTP server. The thing is that I used passive mode with the method

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

enterLocalPassiveMode() 

Recently I was told that no passive mode is allowed on the FTP server, so I should make my application work in active mode. I suppose it couldn't be done by simply changing the method to

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

enterLocalActiveMode()

What else should I change in the application to ensure it's working in active mode.

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

Here's a code snippet which makes the connection to the server:

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

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

Some guidance to what I have to change?

对我必须改变什么的一些指导?

回答by Zach Thacker

http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html#enterLocalActiveMode()

http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPClient.html#enterLocalActiveMode()

Not sure if this is the FTPClient you're using but it would appear that enterLocalActiveMode does indeed exist.

不确定这是否是您正在使用的 FTPClient,但似乎 enterLocalActiveMode 确实存在。

回答by Keerthivasan

FTPClientDocumentation states that

FTPClient文件指出

public boolean enterRemoteActiveMode(InetAddress host,int port) throws IOException

public boolean enterRemoteActiveMode(InetAddress host,int port) throws IOException

Set the current data connection mode to ACTIVE_REMOTE_DATA_CONNECTION . Use this method only for server to server data transfers. This method issues a PORT command to the server, indicating the other server and port to which it should connect for data transfers. You must call this method before EVERY server to server transfer attempt. The FTPClient will NOT automatically continue to issue PORT commands. You also must remember to call enterLocalActiveMode() if you wish to return to the normal data connection mode.

将当前数据连接模式设置为 ACTIVE_REMOTE_DATA_CONNECTION 。此方法仅用于服务器到服务器的数据传输。此方法向服务器发出 PORT 命令,指示它应连接到的其他服务器和端口以进行数据传输。您必须在每个服务器到服务器传输尝试之前调用此方法。FTPClient 不会自动继续发出 PORT 命令。如果您希望返回正常数据连接模式,您还必须记住调用 enterLocalActiveMode()。

Hope this helps!

希望这可以帮助!

回答by Cole Pram

This is old, but I stumbled onto it trying to solve the issue myself.

这是旧的,但我偶然发现它试图自己解决这个问题。

You have to call enterLocalPassiveMode() after calling connect() and before calling login().

您必须在调用 connect() 之后和调用 login() 之前调用 enterLocalPassiveMode()。

See my example below which initialises the FTPClient in local passive mode, lists files for a given directory, then closes the connections.

请参阅下面的示例,该示例以本地被动模式初始化 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;
}