'Downloading Files from SFTP using jsch
I am using JSCH to download files from SFTP server. I am using single session, with multiple channels to download files from different folders located in SFTP. For this downloading process I have a set of scheduled jobs. Each job will:
- open a new channel (
ChannelSftp) everytime. channel name : sftp - uses method
ChannelSftp.ls()to get the size of total number of files to download - If size(Vector) is greater than zero then uses
ChannelSftp.get(remotedir/'*.*', localdir)to download all the files - finally closes the opened channel.
During the above process most of the times I am getting File Not Found or No Such File Exceptions and not downloading some files.
Can anyone please suggest me why it will happen. What may be the cause. How to resolve this problem
below is the code I am using:
ChannelSftp channelSftp = null;
try {
channelSftp = getChannelConnectionUtil().openChannel(); //SFTPConnection.getSession().openChannel("sftp");
@SuppressWarnings("rawtypes")
Vector numOfFiles = channelSftp.ls(ftpDir+"/*.*");
if(numOfFiles.size() > 0){
channelSftp.get(ftpDir+"/*.*",localDir); // Here I am getting error
}
} catch (Exception e) {
e.printStackTrace();
} finally {
getChannelConnectionUtil().disconnectChannel(channelSftp);
}
Solution 1:[1]
Without your code it is hard to diagnose the issue. I suggest forgetting the vector size check, simply iterate through your vector list and count the number of files grabbed. Here is the block of code I use to check for and download files from a remote host:
try {
ChannelSftp c = (ChannelSftp) channel;
c.lcd(localDir);
logger.info("lcd " + c.lpwd());
// Get a listing of the remote directory
@SuppressWarnings("unchecked")
Vector<ChannelSftp.LsEntry> list = c.ls(".");
logger.info("ls .");
// iterate through objects in list, identifying specific file names
for (ChannelSftp.LsEntry oListItem : list) {
// output each item from directory listing for logs
logger.info(oListItem.toString());
// If it is a file (not a directory)
if (!oListItem.getAttrs().isDir()) {
// Grab the remote file ([remote filename], [local path/filename to write file to])
logger.info("get " + oListItem.getFilename());
c.get(oListItem.getFilename(), oListItem.getFilename()); // while testing, disable this or all of your test files will be grabbed
grabCount++;
// Delete remote file
//c.rm(oListItem.getFilename()); // Deleting remote files is not requried in every situation.
}
}
// Report files grabbed to log
if (grabCount == 0) {
logger.info("Found no new files to grab.");
} else {
logger.info("Retrieved " + grabCount + " new files.");
}
} catch(SftpException e) {
logger.warning(e.toString());
} finally {
// disconnect session. If this is not done, the job will hang and leave log files locked
session.disconnect();
logger.info("Session Closed");
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Damienknight |
