java - 使用 JSch 列出 SFTP 中的前 N ​​个文件

标签 java sftp jsch

我需要获取 SFTP 文件夹中文件名的递归列表。现在我使用以下代码:

private void getFilesRecursive(List<LsEntry> fileList, List<FileDto> response, String dirParentName,
        SftpManagerWithPool manager) throws SftpException {
    for (LsEntry file : fileList) {
        if (!file.getAttrs().isDir()) {
            response.add(new FileDto(file.getFilename(), StorageType.FOLDER.getName(),
                    new Attributes(file.getAttrs().getATime(), file.getAttrs().getMTime(),
                            file.getAttrs().getAtimeString(), file.getAttrs().getMtimeString(),
                            file.getAttrs().getPermissionsString(), getPathWitoutRootDirectoryt(dirParentName),
                            file.getAttrs().getSize(), file.getAttrs().isDir())));
        } else if (file.getAttrs().isDir() && !".".equals(file.getFilename())) {
            List<LsEntry> files = manager.listFiles(context.getBasePath().concat("/")
                    .concat(dirParentName.concat("/").concat(file.getFilename())));
            getFilesRecursive(files, response, dirParentName.concat("/").concat(file.getFilename()), manager);
        }
    }
}

public List<FileDto> getFiles() throws ServiceException {
    Session session = null;
    SftpManagerWithPool manager = null;
    try {
        session = getSession();
        manager = new SftpManagerWithPool(session);
        manager.connect();
        List<FileDto> response = new ArrayList<>();
        List<LsEntry> files = manager
                .listFiles(context.getBasePath().concat("/").concat(context.getPathToProcess()));
        getFilesRecursive(files, response, context.getPathToProcess(), manager);
        return response;
    } catch (Exception e) {
        throw new ServiceException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(),
                e.getLocalizedMessage());
    } finally {
        if (manager != null) {
            manager.disconnect();
            try {
                returnSession(session);
            } catch (Exception e) {
                throw new ServiceException(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage(),
                        e.getLocalizedMessage());
            }
        }
    }
}

以下是 SftpManagerWithPool 中的方法:

@SuppressWarnings("unchecked")
public List<LsEntry> listFiles(String pathFrom) throws SftpException {
    if (c == null || session == null || !session.isConnected() || !c.isConnected()) {
        throw new SftpException(ErrorCode.LIST_FILES.ordinal(), "Connection to server is closed. Open it first.");
    }
    String filePath = pathFrom + "/";
    return c.ls(filePath);
}

一切都适用于不超过 5k 的文件,但是当我们有更多文件时,它会花费很多时间并导致超时。

我的问题是,如何使用 c.ls(filePath); 方法只列出文件夹中的前 N ​​个文件?我正在寻找类似于 Linux shell 命令的东西 ls -U |头-4

----编辑-----

我修改了我的方法 listFiles 如下,但我仍然没有获得更好的性能:

public List<LsEntry> listFiles(String pathFrom, int top) throws SftpException {
        if (c == null || session == null || !session.isConnected() || !c.isConnected()) {
            throw new SftpException(ErrorCode.LIST_FILES.ordinal(), "Connection to server is closed. Open it first.");
        }
        String filePath = pathFrom + "/";
        List<LsEntry> response = new ArrayList<>();
        c.ls(filePath, new LsEntrySelector() {

            @Override
            public int select(LsEntry record) {
                if (response.size() <= top) {
                    response.add(record);
                    return LsEntrySelector.CONTINUE;
                } else {
                    return LsEntrySelector.BREAK;
                }
            }
        });
        return response;
    }

非常感谢:)

最佳答案

使用这个overload of ChannelSftp.ls method that takes LsEntrySelector interface :

public void ls(String path, LsEntrySelector selector)

实现LsEntrySelector.select收集文件条目。一旦你有足够的它们,让它返回 BREAK

关于java - 使用 JSch 列出 SFTP 中的前 N ​​个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43238124/

相关文章:

java - 如何在 Java 中运行 Linux 命令时只显示结果?

java - 我可以运行 Hadoop onflow (在应用程序运行时运行 Map Reduce)

Ruby SFTP——删除目录

java - wait() 不强制线程等待?

Sass watch 命令在完整的 sftp 上传之前编译 .scss 文件

c++ - 使用 Qt 安全上传文件

java - 使用 Jsch 将带有目录的文件传输到 SFTP 服务器

java - 获取用于 JSch 的文件时出现权限错误

java - 在 Java 中创建 JTable

Java ORM : Multiple (interface) inheritance