使用Apache Commons VFS-SFTP上传到服务器

3

我正在尝试使用Apache Commons VFS将文件通过SFTP上传到服务器,但是我一直收到以下错误:

java.lang.RuntimeException: org.apache.commons.vfs2.FileSystemException: Could not connect to SFTP server at "sftp://user:***@xxx.x.xxx.xxx/".

这里没有包含远程文件路径(remoteFilePath),这正常吗?我的代码中已经将它包含在连接字符串中了(见下文)。
我在pom中包含了以下jar包:
- commons-logging-1.1.3.jar - commons-vfs2-2.0.jar - hamcrest-core-1.3.jar - jsch-0.1.50.jar 代码如下:
public void SftpMethod(String strMsg, String tableName){

    String host = "xxx.x.xxx.xxx";
    String user = "user";
    String pass = "password!";
    String localFilePath = "C:\\Users\\exampleDir\\Desktop\\loc.dat";
    String remoteFilePath = "/dir/home/user/export/loc.dat";
    StandardFileSystemManager manager = new StandardFileSystemManager();

    File file = new File(localFilePath);

    if (!file.exists())
        throw new RuntimeException("Error. Local file not found");

    try{
        manager.init();
        // Create local file object
        FileObject localFile = manager.resolveFile(file.getAbsolutePath());
        // Create remote file object
        FileObject remoteFile = manager.resolveFile(
                createConnectionString(host, user, pass, remoteFilePath), 
                createDefaultOptions());
        // Copy local file to SFTP server
        remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
        System.out.println("File upload success");

    }catch(IOException e){
        throw new RuntimeException(e);
    }finally{
        manager.close();
    }
}

public static String createConnectionString(String hostName, String username, String password, String remoteFilePath) {
    return "sftp://" + username + ":" + password + "@" + hostName + "/" + remoteFilePath;
}

public static FileSystemOptions createDefaultOptions() throws FileSystemException {
    // Create SFTP options
    FileSystemOptions opts = new FileSystemOptions();

    // SSH Key checking
    SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");

    /*
     * Using the following line will cause VFS to choose File System's Root
     * as VFS's root. If I wanted to use User's home as VFS's root then set
     * 2nd method parameter to "true"
     */
    // Root directory set to user home
    SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);

    // Timeout is count by Milliseconds
    SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);

    return opts;
}

1
@Ram,感谢您的编辑建议。 - TCRISP
欢迎。希望你的问题能够尽快得到解答。祝你好运。 - Ram
你能否包含完整的异常堆栈跟踪? - Martin Prikryl
我遇到了相同的问题。你解决了这个问题吗? - Sam
1个回答

2
没有完整的堆栈跟踪,很难给出最终答案,但这是我最近看到的情况:
Caused by: org.apache.commons.vfs2.FileSystemException: Could not load private key from "/Users/<user>/.ssh/id_rsa".
    at org.apache.commons.vfs2.provider.sftp.SftpClientFactory.createConnection(SftpClientFactory.java:131)

很遗憾,我并不想使用公钥/私钥。我只是想用用户名和密码登录。我需要一种方法来停止尝试读取我的私钥。

根本原因是代码正在使用默认位置来读取我的密钥,即使这不是我想要的。

因此,解决方法是通过设置以下属性来覆盖默认位置:

 System.setProperty("vfs.sftp.sshdir", "/");

这样就绕过了读取ssh密钥的尝试,成功连接。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接