使用jsch 从 SFTP 下载文件
原标题: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);
}
问题回答
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 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");
}
相关问题
Spring Properties File
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
What do you say of chopping type-4 UUID in this manner
Check this,
List<String> list = new ArrayList<String>();
for (int i = 0; i < 10000; i++) {
String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
combining decorator and state pattern in java - question about OO design
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
Unable to execute stored Procedure using Java and JDBC on SQL server
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
Logging a global ID in multiple components
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications.
[App A] -> [App B] -> [App C]
We set a ...
Java Library Size
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is:
How will the larger, mostly unused ...
How to get the Array Class for a given Class in Java?
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this:
Class arrayOfFooClass = java.lang....
SQLite , Derby vs file system
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database.
How hard is it to migrate ...