删除远程服务器上的文件
同之前的说明,先在工具类中添加一个删除远程文件的方法
public static void rmFile(String host, String username, String password,
String remoteFile, int port) throws IOException {
if (logger.isInfoEnabled()) {
logger.info("rm [" + remoteFile + "] is " + host + remoteFile);
}
Connection conn = null;
try {
conn = getOpenedConnection(host, username, password, port);
SFTPv3Client sftpClient = new SFTPv3Client(conn);
sftpClient.rm(remoteFile);sftpClient.close();
} finally { if( null != conn ) { conn.close(); } } }
首先在服务器的/usr/local下面新建一个test.txt文件,如图所示:
public static void main(String[] args) { String remoteFile = "/usr/local/test.txt"; try { CommandRunner.rmFile("172.16.18.141", "root", "123456", remoteFile, 22); } catch (IOException e) { e.printStackTrace(); } }
log4j:WARN No appenders could be found for logger (com.ssh2.shell.ganymed.CommandRunner).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
查看服务器目录:
文件已经删除,至此,删除服务器文件的方法实现完成。