use of com.jcraft.jsch.SftpException in project ats-framework by Axway.
the class SftpClient method performDownloadFile.
@Override
protected void performDownloadFile(String localFile, String remoteDir, String remoteFile) throws FileTransferException {
FileOutputStream fos = 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);
// download the file
File file = new File(localFile);
fos = new FileOutputStream(localFile);
if (isDebugMode() && debugProgressMonitor != null) {
debugProgressMonitor.setTransferMetadata(localFile, remoteFile, file.length());
this.channel.get(remoteFile, fos, debugProgressMonitor);
} else {
this.channel.get(remoteFile, fos);
}
// 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 download " + localFile, e);
throw new FileTransferException(e);
} catch (FileNotFoundException e) {
log.error("Unable to create " + localFile, e);
throw new FileTransferException(e);
} finally {
// close the file output stream
IoUtils.closeStream(fos, "Unable to close the file stream after successful download!");
}
if (remoteDir != null && !remoteDir.endsWith("/")) {
remoteDir += "/";
}
log.info("Successfully downloaded '" + localFile + "' from '" + remoteDir + remoteFile + "', host " + this.hostname);
}
use of com.jcraft.jsch.SftpException in project bamboobsc by billchen198318.
the class SFtpClientUtils method get.
/**
* 抓遠端檔案然後存到本機 , 多筆
*
* @param user
* @param password
* @param addr
* @param port
* @param remoteFile
* @param localFile
* @throws JSchException
* @throws SftpException
* @throws Exception
*/
public static void get(String user, String password, String addr, int port, List<String> remoteFile, List<String> localFile) throws JSchException, SftpException, Exception {
Session session = getSession(user, password, addr, port);
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
try {
for (int i = 0; i < remoteFile.size(); i++) {
String rf = remoteFile.get(i);
String lf = localFile.get(i);
logger.info("get remote file: " + rf + " write to:" + lf);
sftpChannel.get(rf, lf);
File f = new File(lf);
if (!f.exists()) {
f = null;
logger.error("get remote file:" + rf + " fail!");
throw new Exception("get remote file:" + rf + " fail!");
}
f = null;
logger.info("success write:" + lf);
}
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
sftpChannel.exit();
channel.disconnect();
session.disconnect();
}
}
use of com.jcraft.jsch.SftpException in project bamboobsc by billchen198318.
the class SFtpClientUtils method rm.
public static void rm(String user, String password, String addr, int port, List<String> fileName) throws JSchException, SftpException, Exception {
if (fileName == null || fileName.size() < 1) {
return;
}
Session session = getSession(user, password, addr, port);
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
try {
for (String f : fileName) {
sftpChannel.rm(f);
logger.warn("success remove file from " + addr + " :" + fileName);
}
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
sftpChannel.exit();
channel.disconnect();
session.disconnect();
}
}
use of com.jcraft.jsch.SftpException in project cdap by caskdata.
the class SFTPFileSystem method getFileStatus.
/**
* 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.
*/
@SuppressWarnings("unchecked")
private FileStatus getFileStatus(ChannelSftp client, Path file) throws IOException {
FileStatus fileStat = null;
Path workDir;
try {
workDir = new Path(client.pwd());
} catch (SftpException e) {
throw new IOException(e);
}
Path absolute = makeAbsolute(workDir, file);
Path parentPath = absolute.getParent();
if (parentPath == null) {
// root directory
// Length of root directory on server not known
long length = -1;
boolean isDir = true;
int blockReplication = 1;
// Block Size not known.
long blockSize = DEFAULT_BLOCK_SIZE;
// Modification time of root directory not known.
long modTime = -1;
Path root = new Path("/");
return new FileStatus(length, isDir, blockReplication, blockSize, modTime, root.makeQualified(this.getUri(), this.getWorkingDirectory()));
}
String pathName = parentPath.toUri().getPath();
Vector<LsEntry> sftpFiles;
try {
sftpFiles = (Vector<LsEntry>) client.ls(pathName);
} catch (SftpException e) {
throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file));
}
if (sftpFiles != null) {
for (LsEntry sftpFile : sftpFiles) {
if (sftpFile.getFilename().equals(file.getName())) {
// file found in directory
fileStat = getFileStatus(client, sftpFile, parentPath);
break;
}
}
if (fileStat == null) {
throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file));
}
} else {
throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file));
}
return fileStat;
}
use of com.jcraft.jsch.SftpException in project cdap by caskdata.
the class SFTPFileSystem method create.
/**
* A stream obtained via this call must be closed before using other APIs of
* this class or else the invocation will block.
*/
@Override
public FSDataOutputStream create(Path f, FsPermission permission, boolean overwrite, int bufferSize, short replication, long blockSize, Progressable progress) throws IOException {
final ChannelSftp client = connect();
Path workDir;
try {
workDir = new Path(client.pwd());
} catch (SftpException e) {
throw new IOException(e);
}
Path absolute = makeAbsolute(workDir, f);
if (exists(client, f)) {
if (overwrite) {
delete(client, f, false);
} else {
disconnect(client);
throw new IOException(String.format(E_FILE_EXIST, f));
}
}
Path parent = absolute.getParent();
if (parent == null || !mkdirs(client, parent, FsPermission.getDefault())) {
parent = (parent == null) ? new Path("/") : parent;
disconnect(client);
throw new IOException(String.format(E_CREATE_DIR, parent));
}
OutputStream os;
try {
client.cd(parent.toUri().getPath());
os = client.put(f.getName());
} catch (SftpException e) {
throw new IOException(e);
}
FSDataOutputStream fos = new FSDataOutputStream(os, statistics) {
@Override
public void close() throws IOException {
super.close();
disconnect(client);
}
};
return fos;
}
Aggregations