简体   繁体   English

Java vfs2 FileSystemException 私钥无效。 如何解决?

[英]Java vfs2 FileSystemException invalid privatekey. How to solve it?

In my springboot application, i am transferring a file to sftp.在我的 springboot 应用程序中,我正在将文件传输到 sftp。 I want to authenticate it only with username and password.我只想使用用户名和密码对其进行身份验证。 However my java code still looking for private key to send, and i am getting the bellow error.但是,我的 java 代码仍在寻找要发送的私钥,并且出现以下错误。 How can i solve the below error.我该如何解决以下错误。 What changes do i need in the java class?我需要对 java class 进行哪些更改?

Caused by: org.apache.commons.vfs2.FileSystemException: Could not load private key from "/Users/123456/.ssh/id_rsa".原因:org.apache.commons.vfs2.FileSystemException:无法从“/Users/123456/.ssh/id_rsa”加载私钥。

Caused by: com.jcraft.jsch.JSchException: invalid privatekey: [B@180bc464引起:com.jcraft.jsch.JSchException:私钥无效:[B@180bc464

here is my code:这是我的代码:

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");

I think you might be facing the same issue as "Invalid privatekey" when using JSch try the first answer in this and see if it works.我认为您在使用 JSch 时可能会遇到与“无效的私钥”相同的问题,尝试这里的第一个答案,看看它是否有效。

It's solved with below one line of code:它通过以下一行代码解决:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM