<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-scp</artifactId>
<version>2.5.1</version>
</dependency>
try(SshClient client = SshClient.setUpDefaultClient()) {
try(SimpleScpClient scpClient = new SimpleScpClientImpl(SshClient.wrapAsSimpleClient(client))) {
client.start();
ScpClient scp = scpClient.scpLogin("30.0.0.111", "root", "123456");
scp.upload("E:\\localPath", "/remotePath", ScpClient.Option.Recursive, ScpClient.Option.TargetIsDirectory);
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
说明:
ScpClient.Option.Recursive
: 当 localPath 为目录,且目录中有多层子目录和文件时,该选项会将目录下的所有文件递归的上传至远程目录中。ScpClient.Option.TargetIsDirectory
:该选项,说明 ScpClient.upload 方法的 remote 参数为远端的目录,而不是文件。