【问题标题】:Java vfs2 FileSystemException invalid privatekey. How to solve it?Java vfs2 FileSystemException 私钥无效。如何解决?
【发布时间】:2023-02-14 16:37:37
【问题描述】:

在我的 springboot 应用程序中,我正在将文件传输到 sftp。我只想使用用户名和密码对其进行身份验证。但是,我的 Java 代码仍在寻找要发送的私钥,并且出现以下错误。我该如何解决以下错误。我需要在 java 类中进行哪些更改?

原因:org.apache.commons.vfs2.FileSystemException:无法从“/Users/123456/.ssh/id_rsa”加载私钥。

原因:com.jcraft.jsch.JSchException:私钥无效:[B@180bc464

这是我的代码:

StandardFileSystemManager manager = new StandardFileSystemManager();
            String serverAddress = "test.rebex.net";
            String userId = "demo";
            String password = "password";
            String remoteDirectory = "/IN";
            String filepath = "/Users/1234/Documents/TestNotes.txt";
            File file = new File(filepath);
            manager.init();
            FileSystemOptions opts = new FileSystemOptions();
            SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
            SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
            SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
            String sftpUri = "sftp://" + userId + ":" + password +  "@" + serverAddress + "/" + remoteDirectory + filepath;
            FileObject localFile = manager.resolveFile(file.getAbsolutePath());
            FileObject remoteFile = manager.resolveFile(sftpUri, opts);
            remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
            System.out.println("File upload successful");

【问题讨论】:

    标签: java vfs spring-integration-sftp apache-commons-vfs apache-vfs


    【解决方案1】:

    它通过以下一行代码解决:

    SftpFileSystemConfigBuilder.getInstance().setIdentities(opts, new File[0]);
    

    【讨论】:

      【解决方案2】:

      添加这一行,它是工作!谢谢。 但是 setIdentities() 方法已被弃用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-10
        • 2021-06-25
        • 1970-01-01
        • 2017-01-27
        • 1970-01-01
        • 2012-11-07
        • 2013-02-26
        • 2012-08-30
        相关资源
        最近更新 更多