JavaはLinuxのアップロード&ダウンロードファイルを実現

1837 ワード

/**
*
* @param host
* @param port
* @param username
* @param password
* @param privateKeyFile
* @return
* @throws IOException
*/

public static Connection getSSHConnection(String host,int port,String username,String password,String privateKeyFile) throws IOException {
Connection connection=new Connection(host,port);
connection.connect();

File file=new File(privateKeyFile);
System.out.println(file);
boolean b=connection.authenticateWithPublicKey(username,file,password);
System.out.println(b);
if (b){
return connection;
}else {
System.out.println(" , 、 、 ");
return null;
}
}
/**
*
* @param conn SSH
* @param fileName /opt/scf2/log/loanorder/all.log
* @param localPath :D:\
* @throws IOException
*/

public void getFile(Connection conn, String fileName,String localPath) throws IOException{

SCPClient scpClient=conn.createSCPClient();
//String logPath="cd /opt/scf2/log/loanorder";
//Session session = conn.openSession();
//session.execCommand(logPath);// shell
scpClient.get(fileName,localPath);

}

/**
*
* @param conn SSH
* @param fileName D:\a.txt
* @param localPath :/opt/scf2/log/loanorder
* @throws IOException
*/
public void putFile(Connection conn, String fileName,String localPath) throws IOException{

SCPClient scpClient=conn.createSCPClient();
scpClient.put(fileName,localPath);

}

転載先:https://www.cnblogs.com/banml/p/11378547.html