【发布时间】: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