Ganymed SSH-2 for Java系列10之scpGet
直接上代码:
/**
*
* Get remote file through scp
*
* @param host
*
* @param username
*
* @param password
*
* @param remoteFile
*
* @param localDir
*
* @throws IOException
*/
public static void scpGet(String host, String username, String password,
String remoteFile, String localDir, int port) throws IOException {
if (logger.isInfoEnabled()) {
logger.info("spc [" + remoteFile + "] from " + host + " to "
+ localDir);
}
Connection conn = null;
try {
conn = getOpenedConnection(host, username, password, port);
SCPClient client = new SCPClient(conn);
client.get(remoteFile, localDir);
} finally {
if (null != conn) {
conn.close();
}
}
}
测试:
String remoteDir = "/usr/local/test.log";
String localFile ="src/";
try {
CommandRunner.scpGet("172.16.18.141", "root",
"123456", remoteDir,localFile, 22);
} catch (IOException e) {
e.printStackTrace();
}
通过查看src目录下面,发现文件已经get下来了。