'Get file or all folder from SFTP without save it localy

I have functionality (Java 8 app) to read files from FTP by Apache Commons

 FTPClient ftp = new FTPClient();
        ftp.connect(
                FtpProperties.getHost(), 
                FtpProperties.getPort()
        );
        boolean login = ftp.login(
                FtpProperties.getUname(), 
                FtpProperties.getPass()
        );
        ftp.enterLocalPassiveMode();
        ftp.changeWorkingDirectory("/files");
        FTPFile[] files = ftp.listFiles();

Files are not explicitly saved locally anywhere. I download to the files[] by ftp.listFiles(). In my files[] i have of all files from ftp directtory. And I can display them normally, save them, do whatever I want with them.

Does anyone know of an analogous solution for SFTP ?

SOLUTION:

public static void ls() throws Exception {
        JSch jsch = new JSch();
        Session session = jsch.getSession("user", "host", port);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setPassword("password");
        session.connect();

        Channel channel = session.openChannel("sftp");
        channel.connect();
        ChannelSftp sftpChannel = (ChannelSftp) channel;

        Vector<ChannelSftp.LsEntry> returnVector = sftpChannel.ls("/files");
        List<String> returnList = new ArrayList<>();
        for (ChannelSftp.LsEntry item : returnVector) {
            returnList.add(item.getFilename());
        }
        System.out.println(returnList);
    }

System.out.println(returnList); result in console:

[myfile.txt]

This is it what i wanted to achieve. Thanks Rob !



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source