use of com.jcraft.jsch.SftpException in project hadoop by apache.
the class SFTPFileSystem method delete.
/**
* Convenience method, so that we don't open a new connection when using this
* method from within another method. Otherwise every API invocation incurs
* the overhead of opening/closing a TCP connection.
*/
private boolean delete(ChannelSftp channel, Path file, boolean recursive) throws IOException {
Path workDir;
try {
workDir = new Path(channel.pwd());
} catch (SftpException e) {
throw new IOException(e);
}
Path absolute = makeAbsolute(workDir, file);
String pathName = absolute.toUri().getPath();
FileStatus fileStat = null;
try {
fileStat = getFileStatus(channel, absolute);
} catch (FileNotFoundException e) {
// file not found, no need to delete, return true
return false;
}
if (!fileStat.isDirectory()) {
boolean status = true;
try {
channel.rm(pathName);
} catch (SftpException e) {
status = false;
}
return status;
} else {
boolean status = true;
FileStatus[] dirEntries = listStatus(channel, absolute);
if (dirEntries != null && dirEntries.length > 0) {
if (!recursive) {
throw new IOException(String.format(E_DIR_NOTEMPTY, file));
}
for (int i = 0; i < dirEntries.length; ++i) {
delete(channel, new Path(absolute, dirEntries[i].getPath()), recursive);
}
}
try {
channel.rmdir(pathName);
} catch (SftpException e) {
status = false;
}
return status;
}
}
use of com.jcraft.jsch.SftpException in project ats-framework by Axway.
the class SftpClient method performUploadFile.
@Override
protected void performUploadFile(String localFile, String remoteDir, String remoteFile) throws FileTransferException {
FileInputStream fis = null;
try {
// change to correct remote directory, but save the one that the
// client was in before that
String currentDir = this.channel.pwd();
this.channel.cd(remoteDir);
// upload the file
File file = new File(localFile);
fis = new FileInputStream(file);
if (synchronizationSftpTransferListener != null) {
this.channel.put(fis, remoteFile, synchronizationSftpTransferListener);
} else if (isDebugMode() && debugProgressMonitor != null) {
debugProgressMonitor.setTransferMetadata(localFile, remoteFile, file.length());
this.channel.put(fis, remoteFile, debugProgressMonitor);
} else {
this.channel.put(fis, remoteFile);
}
// go back to the location that the client was before the upload
// took place
this.channel.cd(currentDir);
} catch (SftpException e) {
log.error("Unable to upload file!", e);
throw new FileTransferException(e);
} catch (FileNotFoundException e) {
log.error("Unable to find the file that needs to be uploaded!", e);
throw new FileTransferException(e);
} finally {
// close the file input stream
IoUtils.closeStream(fis, "Unable to close the file stream after successful upload!");
}
if (remoteDir != null && !remoteDir.endsWith("/")) {
remoteDir += "/";
}
log.info("Successfully uploaded '" + localFile + "' to '" + remoteDir + remoteFile + "', host " + this.hostname);
}
use of com.jcraft.jsch.SftpException in project GNS by MobilityFirst.
the class SFTPUpload method localFileNewer.
/**
*
* @param user
* @param host
* @param keyFile
* @param fileToTransfer
* @param sftpWorkingDirectory
* @return true if the local file is newer
*/
public static boolean localFileNewer(String user, String host, File keyFile, String fileToTransfer, String sftpWorkingDirectory) {
if (verbose) {
System.out.println("Local File Newer Check " + fileToTransfer + " to " + host + "@" + user + " " + sftpWorkingDirectory);
}
try {
ChannelSftp channelSftp = authenticateSftp(user, host, keyFile);
Path paths = Paths.get(fileToTransfer);
String localDir = paths.getParent().toString();
channelSftp.cd(sftpWorkingDirectory);
channelSftp.lcd(localDir);
SftpATTRS remoteAttributes = channelSftp.stat(paths.getFileName().toString());
long localTime = new File(fileToTransfer).lastModified();
long remoteTime = remoteAttributes.getMTime() * 1000L;
if (verbose) {
System.out.println("L: " + localDir + " R: " + sftpWorkingDirectory + "\n" + "Local time = " + localTime + " Remote time = " + remoteTime);
}
if (verbose) {
System.out.println("Result " + (localTime > remoteTime));
}
return localTime > remoteTime;
} catch (JSchException | SftpException e) {
System.out.println("Exception while checking for file newer:" + e);
return false;
}
}
use of com.jcraft.jsch.SftpException in project GNS by MobilityFirst.
the class SFTPUpload method uploadFile.
/**
*
* @param user
* @param host
* @param keyFile
* @param fileToTransfer
* @param sftpWorkingDirectory
*/
public static void uploadFile(String user, String host, File keyFile, String fileToTransfer, String sftpWorkingDirectory) {
if (verbose) {
System.out.println("Upload file from " + fileToTransfer + " to " + host + "@" + user + " " + sftpWorkingDirectory);
}
try {
ChannelSftp channelSftp = authenticateSftp(user, host, keyFile);
File f = new File(fileToTransfer);
channelSftp.put(new FileInputStream(f), f.getName());
} catch (JSchException | SftpException | FileNotFoundException e) {
System.out.println("Exception while uploading file:" + e);
}
}
use of com.jcraft.jsch.SftpException in project azure-tools-for-java by Microsoft.
the class SparkSubmitHelper method sftpFileToEmulator.
public String sftpFileToEmulator(String localFile, String folderPath, IClusterDetail clusterDetail) throws IOException, HDIException, JSchException, SftpException {
EmulatorClusterDetail emulatorClusterDetail = (EmulatorClusterDetail) clusterDetail;
final File file = new File(localFile);
try (FileInputStream fileInputStream = new FileInputStream(file)) {
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) {
String sshEndpoint = emulatorClusterDetail.getSSHEndpoint();
URL url = new URL(sshEndpoint);
String host = url.getHost();
int port = url.getPort();
JSch jsch = new JSch();
Session session = jsch.getSession(emulatorClusterDetail.getHttpUserName(), host, port);
session.setPassword(emulatorClusterDetail.getHttpPassword());
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
String[] folders = folderPath.split("/");
for (String folder : folders) {
if (folder.length() > 0) {
try {
channel.cd(folder);
} catch (SftpException e) {
channel.mkdir(folder);
channel.cd(folder);
}
}
}
channel.put(bufferedInputStream, file.getName());
channel.disconnect();
session.disconnect();
return file.getName();
}
}
}
Aggregations