Reliable example of how to use SFTP using public private key authentication with Java

前端 未结 3 901
生来不讨喜
生来不讨喜 2021-01-31 05:41

Recently a client of our unexpectedly shifted some important files we collect from an ftp to sftp server. Initially I was under the impression that it would be simple to write

3条回答
  •  广开言路
    2021-01-31 06:00

    This post and answer were very helpful, thank you very much.

    I just want to add an integration to the statement "currently the apache-commons-vfs2 library does not support passphrases", as I did it also with passphrase and it worked.

    You have to import the jsch library in your project (I used 0.1.49) and implement the interface "com.jcraft.jsch.UserInfo".

    Something like this should be fine:

    public class SftpUserInfo implements UserInfo {
    
        public String getPassphrase() {
            return "yourpassphrase";
        }
    
        public String getPassword() {
            return null;
        }
    
        public boolean promptPassphrase(String arg0) {
            return true;
        }
    
        public boolean promptPassword(String arg0) {
            return false;
        }
    }
    

    And then you can add it to the SftpFileSystemConfigBuilder this way:

    SftpFileSystemConfigBuilder.getInstance().setUserInfo(fsOptions, new SftpUserInfo());
    

    Hope this helps.

提交回复
热议问题