Ganymed SSH-2 for Java系列4之删除远程服务器上的文件

16 篇文章 0 订阅
14 篇文章 0 订阅

删除远程服务器上的文件

同之前的说明,先在工具类中添加一个删除远程文件的方法


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.


查看服务器目录:



文件已经删除,至此,删除服务器文件的方法实现完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值