English 中文(简体)
使用jsch 从 SFTP 下载文件
原标题:Downloading Files from SFTP using jsch
  • 时间:2013-06-15 13:08:12
  •  标签:
  • java
  • 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 ...

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 ...

热门标签