微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用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);
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。