在我的springboot应用程序中,我正在将一个文件传输到sftp。我只想用用户名和密码进行身份验证。然而,我的java代码仍在寻找要发送的私钥,我得到了以下错误。我该如何解决以下错误。我需要对java类进行哪些更改?
原因:org.apache.commons.fs2.FileSystemException:无法从"加载私钥/用户/123456/.ssh/id_rsa";。
引起原因:com.jcraf.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");
只需以下一行代码即可解决:
SftpFileSystemConfigBuilder.getInstance().setIdentities(opts, new File[0]);