java 如何使用SftpFileSystemConfigBuilder.getInstance().setIdentityProvider()

xdnvmnnf  于 2023-04-19  发布在  Java
关注(0)|答案(1)|浏览(100)

我使用Apache vfs2连接到SFTP服务器,但setIdentities()方法已被弃用,所以我想改用setIdentities()方法,但我得到了NullPointerException,如何更改我的代码来修复它?谢谢。

public SFtpUtil(String hostName, String portNo, String userId, String userPwd) {
        sftpUri = getSftpUri(hostName, portNo, userId, userPwd);
        log.info("sftpUri=" + sftpUri);
        FileSystemOptions opts = new FileSystemOptions();
try {
                        
    SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
    SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
    SftpFileSystemConfigBuilder.getInstance().setConnectTimeout(opts, Duration.ofSeconds(10000));

// User setIdentities() method, it works, but deprecated
// SftpFileSystemConfigBuilder.getInstance().setIdentities(opts, new File[0]);
            
// I follow the suggestion to use setIdentityProvider()
SftpFileSystemConfigBuilder.getInstance().setIdentityProvider(opts, new IdentityInfo(null));
            log.info("setting SftpFileSystemConfigBuilder finished.");
INFO | setting SftpFileSystemConfigBuilder finished.
 INFO | create manager
 INFO | manager init.
org.apache.commons.vfs2.FileSystemException: Could not connect to SFTP server at "sftp://xxxxxxxxxxxxx...../".
    at org.apache.commons.vfs2.provider.sftp.SftpFileProvider.createSession(SftpFileProvider.java:72)
    at org.apache.commons.vfs2.provider.sftp.SftpFileProvider.doCreateFileSystem(SftpFileProvider.java:92)
    at org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider.getFileSystem(AbstractOriginatingFileProvider.java:92)
    at org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider.findFile(AbstractOriginatingFileProvider.java:71)
    at org.apache.commons.vfs2.provider.AbstractOriginatingFileProvider.findFile(AbstractOriginatingFileProvider.java:55)
    at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:788)
    at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:835)
    at lab.tmp.RunSftp.main(RunSftp.java:19)
Caused by: java.lang.NullPointerException
    at com.jcraft.jsch.Util.checkTilde(Util.java:489)
    at com.jcraft.jsch.Util.fromFile(Util.java:506)
    at com.jcraft.jsch.KeyPair.load(KeyPair.java:540)
    at com.jcraft.jsch.IdentityFile.newInstance(IdentityFile.java:40)
    at com.jcraft.jsch.JSch.addIdentity(JSch.java:423)
    at org.apache.commons.vfs2.provider.sftp.IdentityInfo.addIdentity(IdentityInfo.java:87)
    at org.apache.commons.vfs2.provider.sftp.SftpClientFactory.addIdentity(SftpClientFactory.java:117)
    at org.apache.commons.vfs2.provider.sftp.SftpClientFactory.addIdentities(SftpClientFactory.java:104)
    at org.apache.commons.vfs2.provider.sftp.SftpClientFactory.createConnection(SftpClientFactory.java:154)
    at org.apache.commons.vfs2.provider.sftp.SftpFileProvider.createSession(SftpFileProvider.java:65)
    ... 8 more

有人可以帮助我修复或告诉我如何更改我的代码。

ldxq2e6h

ldxq2e6h1#

据我所知,使用IdentifyInfo需要提供私钥。假设您的私钥位于/.ssh/id_rsa中,则:

File keyFile = new File(System.getProperty("user.home") + "/.ssh/id_rsa");
IdentityProvider identityInfo = new IdentityInfo(keyFile);
builder.setIdentityProvider(opts, identityInfo);

然而,虽然我通过指定用户帐户名和密码的身份验证来实现这一点,但我无法使用ssh密钥来实现这一方法。

相关问题