【发布时间】:2018-12-21 16:24:45
【问题描述】:
我正在尝试列出目录中的所有 *.xml 文件。我先做了一个cd,然后尝试执行:
find . -type f -name *.xml
但不知道具体怎么做。 Exec 通道周围有一些示例,但有没有办法使用 SFTP 本身进行查找?
String username = "abcd";
String password = "pqrst";
String host = "xxxxxx.xxxx.xxx";
int port = 22;
String SFTPWORKINGDIR = "/xxx/xxx/xxx/xxxx";
Session session = null;
ChannelSftp channelSftp = null;
try {
JSch jSch = new JSch();
session = jSch.getSession(username, host, port);
session.setConfig("StrictHostKeyChecking", "no");
session.connect();
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
channelSftp.cd(SFTPWORKINGDIR);
// List all the *xml file.
// --------- Want to execute 'find . -type f -name "*.xml" ' here ---------
/* Vector fileList = channelSftp.ls()
for(int i=0; i<fileList.size();i++){
LsEntry entry = (LsEntry) fileList.get(i);
System.out.println(entry.getFilename());
}*/
} catch (JSchException | SftpException e) {
e.printStackTrace();
}
finally {
if(session != null) session.disconnect();
if(channelSftp != null) channelSftp.disconnect();
}
【问题讨论】: