java连接并免密登录SFTP服务器
pubKeyPath 为本地免密访问远程服务器配置的私钥的路径
@Service
public class FtpUtil {
public static ThreadLocal<ChannelSftp> channelSftpMap = new ThreadLocal<ChannelSftp>();
/**
* 连接并登录SFTP服务器(免密登录)
* @param hostname FTP地址
* @param username FTP登录用户
* @param pubKeyPath 指定本机的私钥的路径
* @param timeout 超时时间,单位ms,it use java.net.Socket.setSoTimeout(timeout)
* @return True if successfully completed, false if not.
*/
public boolean loginSFTP(String hostname, int port, String username, String pubKeyPath, int timeout){
ChannelSftp channelSftp = channelSftpMap.get();
if(null!=channelSftp && channelSftp.isConnected()){
return true;
}
channelSftpMap.remove();
JSch jsch = new JSch();