channelsftp的put_Java中com.jcraft.jsch.ChannelSftp讲解

http://blog.csdn.net/allen_zhao_2012/article/details/7941631

http://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html

http://xpenxpen.iteye.com/blog/2061869

http://blog.csdn.net/fyqcdbdx/article/details/23863793

http://blog.csdn.net/u013256816/article/details/52701563?utm_source=gold_browser_extension

SFTP是Secure File Transfer Protocol的缩写,安全文件传送协议。可以为传输文件提供一种安全的加密方法。SFTP 为 SSH的一部份,是一种传输文件到服务器的安全方式。SFTP是使用加密传输认证信息和传输的数据,所以,使用SFTP是非常安全的。但是,由于这种传输方式使用了加密/解密技术,所以传输效率比普通的FTP要低得多,如果您对网络安全性要求更高时,可以使用SFTP代替FTP。

ChannelSftp类是JSch实现SFTP核心类,它包含了所有SFTP的方法,如:

put():      文件上传

get():      文件下载

cd():       进入指定目录

ls():       得到指定目录下的文件列表

rename():   重命名指定文件或目录

rm():       删除指定文件

mkdir():    创建目录

rmdir():    删除目录

等等(这里省略了方法的参数,put和get都有多个重载方法,具体请看源代码,这里不一一列出。)

一个简单的jsch链接Linux并执行命令的utils。

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import com.jcraft.jsch.Channel;

import com.jcraft.jsch.ChannelExec;

import com.jcraft.jsch.JSch;

import com.jcraft.jsch.JSchException;

import com.jcraft.jsch.Session;

public class ShellUtils {

private static JSch jsch;

private static Session session;

/**

* 连接到指定的IP

*

* @throws JSchException

*/

public static void connect(String user, String passwd, String host) throws JSchException {

jsch = new JSch();

session = jsch.getSession(user, host, 22);

session.setPassword(passwd);

java.util.Properties config = new java.util.Properties();

config.put("StrictHostKeyChecking", "no");

session.setConfig(config);

session.connect();

}

/**

* 执行相关的命令

* @throws JSchException

*/

public static void execCmd(String command, String user, String passwd, String host) throws JSchException {

connect(user, passwd, host);

BufferedReader reader = null;

Channel channel = null;

try {

while (command != null) {

channel = session.openChannel("exec");

((ChannelExec) channel).setCommand(command);

channel.setInputStream(null);

((ChannelExec) channel).setErrStream(System.err);

channel.connect();

InputStream in = channel.getInputStream();

reader = new BufferedReader(new InputStreamReader(in));

String buf = null;

while ((buf = reader.readLine()) != null) {

System.out.println(buf);

}

}

} catch (IOException e) {

e.printStackTrace();

} catch (JSchException e) {

e.printStackTrace();

} finally {

try {

reader.close();

} catch (IOException e) {

e.printStackTrace();

}

channel.disconnect();

session.disconnect();

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值