public static void main(String[] args) {
SshClient client = new SshClient();
try {
ConsoleKnownHostsKeyVerification console = new ConsoleKnownHostsKeyVerification();
client.connect("192.168.13.51", 22);//IP和端口
//设置用户名和密码
PasswordAuthenticationClient pwd = new PasswordAuthenticationClient();
pwd.setUsername("root");
pwd.setPassword("passw0rd");
int result = client.authenticate(pwd);
BASE64Encoder encoder = new BASE64Encoder();
if (result == AuthenticationProtocolState.COMPLETE) {//如果连接完成
SftpClient sftp = client.openSftpClient();
List list = sftp.ls("/root");
for (SftpFile f : list) {
if (f.getFilename().equalsIgnoreCase("A16669F409774609BEB_144000820000_10012562.pdf")) {
String name = f.getFilename();
String filename = name.split("\\.")[0];
String fpqqlsh = filename.split("_")[0];
File file = new File("d:\\" + f.getFilename());
FileOutputStream fos = new FileOutputStream(file);
FileAttributes fa = sftp.get(f.getAbsolutePath(), fos);
fos.write(fa.toByteArray());
fos.close();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}