Skip to content

Instantly share code, notes, and snippets.

@raghavj1991
Last active August 29, 2015 13:56
Show Gist options
  • Save raghavj1991/8864424 to your computer and use it in GitHub Desktop.
Save raghavj1991/8864424 to your computer and use it in GitHub Desktop.
Download feature from amazon - ec2 (remote directory) to local directory
package com.sapient.iota
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 FileDownload {
public void download() throws JSchException {
JSch jsch=new JSch();
jsch.addIdentity("THE AMAZON PRIVATE KEY THAT YOU WILL NEED");
Session session=jsch.getSession("amazon_user", "ip", sftpport);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp channelSftp = null;
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp)channel;
try {
channelSftp.cd("the directory where files are located");
channelSftp.get("the name of the file you want to download","local directory name");
} catch (Exception e) {
e.printStackTrace();
}
channel.disconnect();
session.disconnect();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment