How to retrieve a file from a server via SFTP?

后端 未结 16 1535
萌比男神i
萌比男神i 2020-11-22 07:48

I\'m trying to retrieve a file from a server using SFTP (as opposed to FTPS) using Java. How can I do this?

16条回答
  •  深忆病人
    2020-11-22 08:37

    This was the solution I came up with http://sourceforge.net/projects/sshtools/ (most error handling omitted for clarity). This is an excerpt from my blog

    SshClient ssh = new SshClient();
    ssh.connect(host, port);
    //Authenticate
    PasswordAuthenticationClient passwordAuthenticationClient = new PasswordAuthenticationClient();
    passwordAuthenticationClient.setUsername(userName);
    passwordAuthenticationClient.setPassword(password);
    int result = ssh.authenticate(passwordAuthenticationClient);
    if(result != AuthenticationProtocolState.COMPLETE){
         throw new SFTPException("Login to " + host + ":" + port + " " + userName + "/" + password + " failed");
    }
    //Open the SFTP channel
    SftpClient client = ssh.openSftpClient();
    //Send the file
    client.put(filePath);
    //disconnect
    client.quit();
    ssh.disconnect();
    

提交回复
热议问题