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

标签 java apache maven sftp vfs

我正在尝试使用 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;
}

最佳答案

没有完整的堆栈跟踪很难给出结论性的答案,但这是我最近看到的:

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)

不幸的是,我并没有尝试使用公钥/私钥。我只是打算使用用户名/密码登录。我需要一种方法让它停止尝试读取我的私钥。

根本原因是代码使用了我的 key 的默认位置,并试图读取它(即使那不是我想要的)。

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

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

这完全绕过了读取 ssh key 的尝试,并成功连接。

关于java - 使用 Apache Commons VFS-SFTP,上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30382546/

相关文章:

java - 如何在JSP代码中获取所请求站点的IP地址?

java - Log4j 格式化 : Is it possible to truncate stacktraces?

apache - 使用 nutch 0.9 创建搜索引擎的问题

php - 无法将远程 Openfiles.exe 数据拉入 PHP

Maven 忽略 settings.xml 文件

java - 在 Android 应用程序的 Java JSch 中从字符串或资源加载私钥

java - 如何跟踪 Java 应用程序中已使用的内存?

macos - 错误 : "[ Error writing/etc/httpd/conf/httpd.conf: No such file or directory ]" while trying to edit "httpd.conf" file and save it back

java - 如何将没有存储库的java库添加到pom文件中?

web-services - 为什么 Tomcat 7 部署的应用程序返回 HTTP 状态 404?