Skip to content

Instantly share code, notes, and snippets.

@adilkurniaramdan
Created May 3, 2013 07:07
Show Gist options
  • Save adilkurniaramdan/5507658 to your computer and use it in GitHub Desktop.
Save adilkurniaramdan/5507658 to your computer and use it in GitHub Desktop.
package com.qbiel.conn;
import org.apache.log4j.Logger;
import com.jcraft.jsch.*;
public class SftpConn {
//log4j
static Logger logger = Logger.getLogger(SftpConn.class);
private static final String HOST = "127.0.0.1";
private static final int PORT = 22;
private static final String USER = "adil";
private static final String PASS = "test";
private static ChannelSftp channelSftp;
public static ChannelSftp getChannel() {
Session session;
Channel channel;
if (channelSftp == null) {
try {
JSch ssh = new JSch();
session = ssh.getSession(USER, HOST, PORT);
session.setPassword(PASS);
java.util.Properties config = new java.util.Properties();
//checking public key disabled
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
logger.info("Success Connect To Server..");
} catch (Exception ex) {
logger.error("Failed Connect To Server because : "
+ ex.getMessage());
}
}
return channelSftp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment