引入maven依赖
<dependency>
<groupId>ch.ethz.ganymed</groupId>
<artifactId>ganymed-ssh2</artifactId>
<version>build210</version>
</dependency>
public static Connection getSSHConnection(String host, int port, String username, String password) throws IOException {
Connection connection=new Connection(host,port);
connection.connect();
boolean b=connection.authenticateWithPassword(username,password);
System.out.println(b);
if (b){
return connection;
}else {
System.out.println("登录连接失败,请检查用户名、密码、私钥文件");
return null;
}
}
public static void getFile(Connection conn, String fileName,String localPath) throws IOException{
SCPClient scpClient=conn.createSCPClient();
scpClient.get(fileName,localPath);
}
## 上传方法
public static void putFile(Connection conn, String fileName,String localPath) throws IOException{
SCPClient scpClient=conn.createSCPClient();
scpClient.put(fileName,localPath);
}
测试
Connection conn=getSSHConnection("ip",端口,"用户名","密码");
// //getFile(conn,"/Hotpoint_591_1617061026_5_20200927105754.hp","E:\\");
// putFile(conn,"本地路径","linux服务器路径");