Using JSch ChannelSftp: How to read multiple files with dynamic names?

社会主义新天地 提交于 2019-12-02 18:38:32

The ls method is the one you need. It returns a vector of LsEntry objects, each of which you can ask about its name.

So, after your channelSftp.cd(SFTPWORKINGDIR);, you could do the following:

Vector<ChannelSftp.LsEntry> list = channelSftp.ls("*.cvs");
for(ChannelSftp.LsEntry entry : list) {
    channelSftp.get(entry.getFilename(), destinationPath + entry.getFilename());
}

(This assumes destinationPath is a local directory name ending with / (or \ in Windows).)

Of course, if you don't want to download the same files again after 15 minutes, you might want to have a list of the local files, to compare them (use a HashSet or similar), or delete them from the server.

Note that ls is case sensitive. This method retrieves all csv files, regardless of the extension case

ArrayList<String> list = new ArrayList<String>();
Vector<LsEntry> entries = sftpChannel.ls("*.*");
for (LsEntry entry : entries) {
    if(entry.getFilename().toLowerCase().endsWith(".csv")) {
        list.add(entry.getFilename());
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!