import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import com.github.pagehelper.util.StringUtil;
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;
import com.jcraft.jsch.SftpException;
import lombok.extern.slf4j.Slf4j;
/**
* 方法一: 连接sftp的链接(linux)
* @param host 地址
* @param port 端口号
* @param username 用户名
* @param password 密码
* @return ChannelSftp sftp的引用类
*/
public static ChannelSftp connectHost(String host, int port, String username, String password){
JSch jsch = new JSch();
try {
Session sshSession = jsch.getSession(username, host, port);
log.info("Session created.");
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
log.info("Session connected.");
log.info("Opening Channel.");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
log.info("Connected to " + host + ":" + port);
return (ChannelSftp)channel;
} catch (Exception e) {
log.error("*******连接ftp地址[{}]-[{}]出现异常***************",host,username, e);
return null;
}
}
/**
* 方法一:关闭sftp连接
* 关闭sftp的链接
* @param channelSftp
*/
public static void disconnectSfptHost(ChannelSftp channelSftp){
if (channelSftp != null) {
if (channelSftp.isConnected()) {
try {
channelSftp.getSession().disconnect();
} catc