使用JSch ChannelSftp:如何列出不区分大小写的文件

问题描述

我想要具有名称文件 File1.txt filE2.txt FIle3.txt

当我将fileMask作为文件传递时,我想列出所有三个文件。 基本上,我想使用channel.ls(path + fileMask,选择器)进行不区分大小写的搜索

解决方法

要在sftp位置搜索特定文件,只需执行以下操作:

    remoteFileName = "fileName_[0-9]{08}.txt"
    try {
        ChannelSftp channelSftp = setupJsch();
        channelSftp.connect();
        Vector ls = channelSftp.ls(remoteFilePath);
        Pattern pattern = Pattern.compile(remoteFileName,Pattern.CASE_INSENSITIVE);
        for (Object entry : ls) {
            ChannelSftp.LsEntry e = (ChannelSftp.LsEntry) entry;
            Matcher m = pattern.matcher(e.getFilename());
            if (m.matches()) {
                //Do something
                channelSftp.get(remoteFilePath + e.getFilename(),getLocalFileDir());
                channelSftp.exit(); //If you want to search for multiple files then do not terminate the connection here
            }
        }
    } catch (JSchException jSchException) {
        LOGGER.info("File download failed :",jSchException);
    } catch (SftpException sftpException) {
        LOGGER.info("File download failed :",sftpException);
    }