use of jp.ossc.nimbus.service.sftp.SFTPException in project nimbus by nimbus-org.
the class SFTPClientImpl method connect.
public void connect(String user, String host, int port, File pemFile, String passphrase) throws SFTPException {
if (jsch != null) {
throw new SFTPException("It is already connected!");
}
jsch = new JSch();
try {
jsch.addIdentity(pemFile.getAbsolutePath(), passphrase);
session = jsch.getSession(user, host, port);
if (configProperties != null) {
session.setConfig(configProperties);
}
if (proxy != null) {
session.setProxy(proxy);
}
if (timeout >= 0) {
session.setTimeout(timeout);
}
if (serverAliveInterval >= 0) {
session.setServerAliveInterval(serverAliveInterval);
}
if (serverAliveCountMax >= 0) {
session.setServerAliveCountMax(serverAliveCountMax);
}
session.connect();
channel = (ChannelSftp) session.openChannel("sftp");
if (fileNameEncoding != null) {
channel.setFilenameEncoding(fileNameEncoding);
}
channel.connect();
if (homeDir != null) {
channel.lcd(homeDir.getPath());
}
} catch (JSchException e) {
if (channel != null) {
channel.disconnect();
channel = null;
}
if (session != null) {
session.disconnect();
session = null;
}
jsch = null;
throw new SFTPException("It failed to connect!", e);
} catch (SftpException e) {
if (channel != null) {
channel.disconnect();
channel = null;
}
if (session != null) {
session.disconnect();
session = null;
}
jsch = null;
throw new SFTPException("It failed to connect!", e);
}
}
use of jp.ossc.nimbus.service.sftp.SFTPException in project nimbus by nimbus-org.
the class SFTPClientImpl method mget.
public File[] mget(String remote, String localDir) throws NoSuchFileSFTPException, SFTPException {
if (channel == null) {
throw new SFTPException("Connection is not established!");
}
try {
String[] fileNames = ls(remote);
if (fileNames.length == 0) {
return new File[0];
}
if (localDir == null) {
localDir = lpwd().getPath();
}
channel.get(remote, localDir);
File[] files = new File[fileNames.length];
for (int i = 0; i < fileNames.length; i++) {
files[i] = new File(localDir, fileNames[i]);
}
return files;
} catch (SftpException e) {
if (e.id == ERROR_ID_NO_SUCH_FILE) {
throw new NoSuchFileSFTPException("It failed to mget! remote=" + remote + ", localDir=" + localDir, e);
}
throw new SFTPException("It failed to mget! remote=" + remote + ", localDir=" + localDir, e);
}
}
Aggregations