In this example, I'll be using JSch library to copy a file from my local server to a remote host.
If using Maven, add the following dependency to your pom.xml file:
<!-- for ssh -->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>${jsch.version}</version>
</dependency>
Define an interface for your remote file operations:
public interface RemoteFileUtilities {
public void copyFile(String filename, HostInfo hostInfo);
public List<String> lsDir(String dir, HostInfo hostInfo);
}
SSH implementation class:
public class SshUtilities implements RemoteFileUtilities {
@Override
public void copyFile(String filename, HostInfo hostInfo) {
JSch jsch = new JSch();
Session session = null;
ChannelSftp channel = null;
try {
session = jsch.getSession(hostInfo.getUsername(),
hostInfo.getHostname(), hostInfo.getPort());
session.setPassword(hostInfo.getPassword());
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
File localFile = new File(filename);
// If you want you can change the directory using the following line.
channel.cd(hostInfo.getPath());
channel.put(new FileInputStream(localFile), localFile.getName());
}
catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
channel.disconnect();
session.disconnect();
}
@Override
public List<String> lsDir(String dir, HostInfo hostInfo) {
JSch jsch = new JSch();
Session session = null;
ChannelSftp channel = null;
try {
session = jsch.getSession(hostInfo.getUsername(), hostInfo.getHostname(), hostInfo.getPort());
session.setPassword(hostInfo.getPassword());
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
return channel.ls(dir);
}
catch (JSchException e) {
e.printStackTrace();
}
catch (SftpException e) {
e.printStackTrace();
}
channel.disconnect();
session.disconnect();
return null;
}
}
A supporting class to hold information related to your host:
public class HostInfo {
private String hostname;
private String username;
private String password;
private String path;
private int port;
public HostInfo(String hostname, String username, String password, String path, int port) {
super();
this.hostname = hostname;
this.username = username;
this.password = password;
this.path = path;
this.port = port;
}
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
And finally, a JUnit test class:
public class SshUtilitiesTest {
@Test
public void test() {
HostInfo hostInfo = new HostInfo("dev-core-ccm-1", "noushin", "nbcg14g~", "/home/noushin/ssh-test", 22);
SshUtilities sshUtils = new SshUtilities();
sshUtils.copyFile("/tmp/log/error.log", hostInfo);
List<String> files = sshUtils.lsDir(hostInfo.getPath(), hostInfo);
if (files != null) {
for (int i = 0; i < files.size(); i++)
assert(files.get(i).contains("error.log"));
}
}
}