Java 使用JSch列出SFTP中的前N个文件

Java 使用JSch列出SFTP中的前N个文件,java,sftp,jsch,Java,Sftp,Jsch,我需要在SFTP文件夹中获得文件名的递归列表。现在我使用以下代码: private void getFilesRecursive(List<LsEntry> fileList, List<FileDto> response, String dirParentName, SftpManagerWithPool manager) throws SftpException { for (LsEntry file : fileList) {

我需要在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());
            }
        }
    }
}
非常感谢:)

使用以下命令:


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

,但它返回void如何获取文件列表?在
LsEntrySelector中收集文件名。选择一些容器(例如
Vector
)。请检查我的编辑,我照你说的做了,看起来很有效,但我仍然没有通过列出文件获得更好的性能,还有其他方法吗?列出文件大概需要3分钟,这是一个不同的问题!不管怎样,3分钟到底是什么时间?第一次调用
需要多长时间。选择
@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);
}
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;
    }
public void ls(String path, LsEntrySelector selector)