62,635
社区成员




import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.log4j.Logger;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class SFTPChannel {
Session session = null;
Channel channel = null;
private static final Logger LOG = Logger.getLogger(SFTPChannel.class
.getName());
public ChannelSftp getChannel(Map<String, String> sftpDetails, int timeout)
throws JSchException {
String ftpHost = sftpDetails.get(SFTPConstants.SFTP_REQ_HOST);
String port = sftpDetails.get(SFTPConstants.SFTP_REQ_PORT);
String ftpUserName = sftpDetails.get(SFTPConstants.SFTP_REQ_USERNAME);
String ftpPassword = sftpDetails.get(SFTPConstants.SFTP_REQ_PASSWORD);
int ftpPort = SFTPConstants.SFTP_DEFAULT_PORT;
if (port != null && !port.equals("")) {
ftpPort = Integer.valueOf(port);
}
JSch jsch = new JSch(); // 创建JSch对象
session = jsch.getSession(ftpUserName, ftpHost, ftpPort); // 根据用户名,主机ip,端口获取一个Session对象
LOG.debug("Session created.");
if (ftpPassword != null) {
session.setPassword(ftpPassword); // 设置密码
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config); // 为Session对象设置properties
session.setTimeout(timeout); // 设置timeout时间
session.connect(); // 通过Session建立链接
LOG.debug("Session connected.");
LOG.debug("Opening Channel.");
channel = session.openChannel("sftp"); // 打开SFTP通道
channel.connect(); // 建立SFTP通道的连接
LOG.debug("Connected successfully to ftpHost = " + ftpHost
+ ",as ftpUserName = " + ftpUserName + ", returning: "
+ channel);
return (ChannelSftp) channel;
}
public void closeChannel() throws Exception {
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}
public static void main(String[] args) {
try {
SFTPChannel sftpChannel = new SFTPChannel();
Map<String, String> sftpDetails = new HashMap<String, String>();
// 设置主机ip,端口,用户名,密码
sftpDetails.put(SFTPConstants.SFTP_REQ_HOST, "192.168.0.197");
sftpDetails.put(SFTPConstants.SFTP_REQ_USERNAME, "zhengjf");
sftpDetails.put(SFTPConstants.SFTP_REQ_PASSWORD, "lmis123");
sftpDetails.put(SFTPConstants.SFTP_REQ_PORT, "22");
String src = "C:\\lmis\\AutoRelease\\data\\tempprogram\\gethash.sh"; // 本地文件名
String dst = "/zhengjf/test/test.sh"; // 目标文件名
ChannelSftp chSftp = sftpChannel.getChannel(sftpDetails, 60000);
chSftp.put(src, dst, ChannelSftp.OVERWRITE);
chSftp.quit();
sftpChannel.closeChannel();
} catch (Exception e) {
e.printStackTrace();
}
}
}