@Override public void close() throws IOException { if (null == sftp) { return; } try { if (null != session) { session.disconnect(); } } catch (final Exception ex) { /*IGNORE*/ logger.warn("Unable to disconnect session due to " + ex); if (logger.isDebugEnabled()) { logger.warn("", ex); } } try { if (null != sftp) { sftp.exit(); } } catch (final Exception ex) { /*IGNORE*/ logger.warn("Unable to disconnect session due to " + ex); if (logger.isDebugEnabled()) { logger.warn("", ex); } } } }
c.exit(); System.out.println("done");
@Override public void close() throws IOException { if (closed) { return; } closed = true; try { if (null != sftp) { sftp.exit(); } } catch (final Exception ex) { logger.warn("Failed to close ChannelSftp due to {}", new Object[] {ex.toString()}, ex); } sftp = null; try { if (null != session) { session.disconnect(); } } catch (final Exception ex) { logger.warn("Failed to close session due to {}", new Object[] {ex.toString()}, ex); } session = null; }
channelSftp.exit(); System.out.println("sftp Channel exited."); channel.disconnect();
import com.jcraft.jsch.*; public class TestJSch { public static void main(String args[]) { JSch jsch = new JSch(); Session session = null; try { session = jsch.getSession("username", "127.0.0.1", 22); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword("password"); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; sftpChannel.get("remotefile.txt", "localfile.txt"); sftpChannel.exit(); session.disconnect(); } catch (JSchException e) { e.printStackTrace(); } catch (SftpException e) { e.printStackTrace(); } } }
sftpChannel.exit(); session.disconnect();
//setup Session here ... session.connect(); ... Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; ...run sftp logic... //close sessions here sftpChannel.exit(); session.disconnect();
/** * 断开连接 */ public void close() { if (cs != null) { cs.exit(); } if (session != null) { session.disconnect(); } cs = null; }
public void downloadFile(String remotePath, String localPath) throws JSchException, SftpException { Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp channelSftp = (ChannelSftp) channel; channelSftp.get(remotePath, localPath); channelSftp.exit(); }
public void upload(Map<String,String> files) throws Exception { JSch jsch = new JSch(); Session session = null; session = jsch.getSession(user, host, 22); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword(password); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftp = (ChannelSftp)channel; for(Map.Entry<String, String> ff : files.entrySet()) { // key - source, value - destination sftp.put(ff.getKey(), ff.getValue()); } sftp.exit(); session.disconnect(); }
ChannelSftp channelSftp = null; try { channelSftp = (ChannelSftp) session.openChannel("sftp"); channelSftp.connect(); is = channelSftp.get(path); // read the contents, etc... } finally { if (channelSftp != null) { channelSftp.exit(); } }
@Override public void close() throws IOException { if (closed) { return; } closed = true; try { if (null != sftp) { sftp.exit(); } } catch (final Exception ex) { logger.warn("Failed to close ChannelSftp due to {}", new Object[] {ex.toString()}, ex); } sftp = null; try { if (null != session) { session.disconnect(); } } catch (final Exception ex) { logger.warn("Failed to close session due to {}", new Object[] {ex.toString()}, ex); } session = null; }
public void uploadFile(InputStream stream, String path, int mode) throws JSchException, SftpException { Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp channelSftp = (ChannelSftp) channel; channelSftp.put(stream, path); channelSftp.chmod(mode, path); channelSftp.exit(); }
public void putFile(String fileName, final String content) throws Exception { JSch jsch = new JSch(); Hashtable config = new Hashtable(); config.put("StrictHostKeyChecking", "no"); JSch.setConfig(config); Session session = jsch.getSession(user, "localhost", sshd.getPort()); session.setPassword(password); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; sftpChannel.cd(baseDir); sftpChannel.put(new ByteArrayInputStream(content.getBytes()), fileName); if (sftpChannel.isConnected()) { sftpChannel.exit(); } if (session.isConnected()) { session.disconnect(); } filesToCleanup.add(FileSystems.getDefault().getPath(baseDir+FileSystems.getDefault().getSeparator()+fileName)); }
Thread connectionThread = new Thread(new Runnable() { public void run() { JSch jsch = new JSch(); Session session = null; session = jsch.getSession("******", "*********",22); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword("******"); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; sftpChannel.exit(); session.disconnect(); } });
JSch jsch = new JSch(); Session session = null; session = jsch.getSession("***", "***.***.***.***",22); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword("****"); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; System.out.println(sftpChannel.pwd()); System.out.println(sftpChannel.lpwd()); sftpChannel.exit(); session.disconnect(); }catch(JSchException e){writeToSDFile(" "+e.toString());}
JSch jsch = new JSch(); Session session = null; try { session = jsch.getSession("ragesh", "10.0.0.1", 22); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword("password"); session.connect(); System.out.println("Connected to session successfully"); Channel channel = session.openChannel("sftp"); channel.connect(); System.out.println("Connected to Channel successfully"); ChannelSftp sftpChannel = (ChannelSftp) channel; // this will read file with the name test.txt and write to remote directory sftpChannel.cd("/root/usr/path"); File f = new File("test.txt"); sftpChannel.put(new FileInputStream(f), f.getName()); // here you can also change the target file name by replacing f.getName() with your choice of name sftpChannel.exit(); session.disconnect(); } catch (JSchException e) { e.printStackTrace(); }
public InputStream getFile(String fileName) throws Exception { JSch jsch = new JSch(); Hashtable config = new Hashtable(); config.put("StrictHostKeyChecking", "no"); JSch.setConfig(config); Session session = jsch.getSession(user, "localhost", sshd.getPort()); session.setPassword(password); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; sftpChannel.cd(baseDir); InputStream inputStream = sftpChannel.get(fileName); if (sftpChannel.isConnected()) { sftpChannel.exit(); } if (session.isConnected()) { session.disconnect(); } return inputStream; }
import com.jcraft.jsch.*; public class TestJSch { public static void main(String args[]) { JSch jsch = new JSch(); Session session = null; try { session = jsch.getSession("username", "127.0.0.1", 22); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword("password"); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; sftpChannel.get("remotefile.txt", "localfile.txt"); sftpChannel.exit(); session.disconnect(); } catch (JSchException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } catch (SftpException e) { e.printStackTrace(); } } }
public static void main(String[] args) { putFile("user", "host", "passwd", "/../test.txt", "C:/test.txt"); } public static void putFile(String username, String host, String password, String remotefile, String localfile){ JSch jsch = new JSch(); Session session = null; try { session = jsch.getSession(username, host, 22); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword(password); session.connect(); Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftpChannel = (ChannelSftp) channel; sftpChannel.put(localfile, remotefile); sftpChannel.exit(); session.disconnect(); } catch (JSchException e) { e.printStackTrace(); } catch (SftpException e) { e.printStackTrace(); } }