【发布时间】:2013-02-19 18:48:26
【问题描述】:
我想使用 JSCH 从 FTP 服务器下载所有文件。
下面是代码sn-p,
List<File> fileList = null;
Vector<ChannelSftp.LsEntry> list = sftpChannel.ls(remoteFolder);
for (ChannelSftp.LsEntry file : list) {
if( getLog().isDebugEnabled() ){
getLog().debug("Retrieved Files from the folder is"+file);
}
if (!(new File(file.getFilename())).isFile()) {
continue;
}
fileList.add(new File(remoteFolder,file.getFilename())) ;
return fileList;
该方法将返回 List,用于另一种使用 sftpChannel.get(src,dest) 从远程服务器下载文件的方法;
请让我知道代码是否正常。 我没有环境可以测试,所以无法确认。 但是我为 FTPClient 编写的代码有些相似,并且可以正常工作。
感谢您的帮助。
【问题讨论】:
-
这行得通。但是为什么你将 List 声明为 null 而不是由 ArrayList 实例化?
-
是什么阻止您设置本地 SFTP 服务器?
-
isDir() 的更短的方法可能是: public boolean isDir(SftpChannel sftpChannel, String path) throws SftpException { boolean result = false;结果 = sftpChannel.lstat(path).isDir();返回结果; }