FTPClient - 如何使用主动模式

pla*_*nbv 5 java ftp mode

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

enterLocalPassiveMode() 
Run Code Online (Sandbox Code Playgroud)

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

enterLocalActiveMode()
Run Code Online (Sandbox Code Playgroud)

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

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

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());
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

小智 7

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

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

请参阅下面的示例,该示例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;
}
Run Code Online (Sandbox Code Playgroud)


Zac*_*ker 0

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

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