在Apache commons SFTP中使用oPo​​rt选项

时间:2015-01-29 16:05:35

标签: apache sftp apache-commons-net

我有一个sftp服务器,我可以使用下面的命令手动连接

sftp -oport = 4022 user@xxxxxx.com

但是我发现用apache commons vfs做同样的困难。

以下是我用来建立与sftp服务器连接的方法。但是它没有工作并因错误“org.apache.commons.vfs2.FileSystemException:无法连接到xxxxxx.com上的SFTP服务器而失败”

public boolean connect(String host, String login, String password,
                       int port) throws Exception {

    //If the client is already connected, disconnect
    if (command != null) {
        disconnect();
    }
    FileSystemOptions fso = new FileSystemOptions();

    try {
        SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(fso,
                                                                           "no");

        session =
                SftpClientFactory.createConnection(host, port, login.toCharArray(),
                                                   password.toCharArray(),
                                                   fso);

        System.out.println("pass");
        Channel channel = session.openChannel("ssh");
        channel.connect();
        command = (ChannelSftp)channel;

    } catch (FileSystemException e) {
        throw e;
       // return false;
    }
    return command.isConnected();
}

请帮我解决这个问题

0 个答案:

没有答案