use of jp.ossc.nimbus.service.sftp.SFTPException in project nimbus by nimbus-org.
the class SFTPClientImpl method get.
public File get(String remote, String local) throws NoSuchFileSFTPException, SFTPException {
if (channel == null) {
throw new SFTPException("Connection is not established!");
}
try {
if (local == null) {
local = lpwd().getPath();
}
channel.get(remote, local);
File remoteFile = new File(remote);
File localFile = new File(local);
return localFile.isDirectory() ? new File(localFile, remoteFile.getName()) : localFile;
} catch (SftpException e) {
if (e.id == ERROR_ID_NO_SUCH_FILE) {
throw new NoSuchFileSFTPException("It failed to get! remote=" + remote + ", local=" + local, e);
}
throw new SFTPException("It failed to get! remote=" + remote + ", local=" + local, e);
}
}
use of jp.ossc.nimbus.service.sftp.SFTPException in project nimbus by nimbus-org.
the class SFTPClientImpl method mput.
public void mput(String local, String remoteDir, String mode) throws SFTPException {
if (channel == null) {
throw new SFTPException("Connection is not established!");
}
if (remoteDir == null) {
remoteDir = "./";
} else if (!remoteDir.endsWith("/")) {
remoteDir += "/";
}
RecurciveSearchFile rsf = new RecurciveSearchFile(lpwd().getPath());
File[] localFiles = rsf.listAllTreeFiles(local);
for (int i = 0; i < localFiles.length; i++) {
if (mode == null) {
put(localFiles[i].getAbsolutePath(), remoteDir + localFiles[i].getName());
} else {
put(localFiles[i].getAbsolutePath(), remoteDir + localFiles[i].getName(), mode);
}
}
}
use of jp.ossc.nimbus.service.sftp.SFTPException in project nimbus by nimbus-org.
the class SFTPClientImpl method ls.
public String[] ls(String path) throws NoSuchFileSFTPException, SFTPException {
if (channel == null) {
throw new SFTPException("Connection is not established!");
}
try {
List entries = channel.ls(path);
String[] fileNames = new String[entries.size()];
for (int i = 0; i < entries.size(); i++) {
ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) entries.get(i);
fileNames[i] = entry.getFilename();
}
return fileNames;
} catch (SftpException e) {
if (e.id == ERROR_ID_NO_SUCH_FILE) {
throw new NoSuchFileSFTPException("It failed to ls! path=" + path, e);
}
throw new SFTPException("It failed to ls! path=" + path, e);
}
}
use of jp.ossc.nimbus.service.sftp.SFTPException in project nimbus by nimbus-org.
the class SFTPClientImpl method lsFile.
public SFTPFile[] lsFile(String path) throws SFTPException {
if (channel == null) {
throw new SFTPException("Connection is not established!");
}
try {
List entries = channel.ls(path);
SFTPFile[] files = new SFTPFile[entries.size()];
for (int i = 0; i < entries.size(); i++) {
ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) entries.get(i);
files[i] = new SFTPFileImpl(entry);
}
return files;
} catch (SftpException e) {
if (e.id == ERROR_ID_NO_SUCH_FILE) {
throw new NoSuchFileSFTPException("It failed to ls! path=" + path, e);
}
throw new SFTPException("It failed to ls! path=" + path, e);
}
}
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, String password) throws SFTPException {
if (jsch != null) {
throw new SFTPException("It is already connected!");
}
jsch = new JSch();
try {
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);
}
if (password != null) {
session.setPassword(password);
}
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);
}
}
Aggregations