开发者

retrieving list of files from SFTP server in java

I want the list of files of a particular directory which is present in sftp server or ftp server.I written following code to do that but it is not working for me.

  FTPClient client = new FTPClient();

    try {
        client.connect("ftp.secureftp-test.com ");
        client.login("test", "test");

        String[] names = client.listNames();
        for (String name : names) {
            System.out.println("Name = " + name);
        }

        FTPFile[] ftpFiles = client.listFiles();
        for (FTPFile ftpFile : ftpFiles) {开发者_如何学Python
            // Check if FTPFile is a regular file
            if (ftpFile.getType() == FTPFile.FILE_TYPE) {
                System.out.println("FTPFile: " + ftpFile.getName() + "; " + FileUtils.byteCountToDisplaySize(ftpFile.getSize()));
            }
        }
        client.logout();
        client.disconnect();
    } catch (Exception e) {
        e.printStackTrace();
    }

I replace the jar of jsse but now above code is not giving any output.It does not returning any file name.

So anybody knows how to get the list of all files from ftp or sftp server using java and only opensource libraries.


Do you know which protocol your server is actually using?

You didn't specify what library you were using, but it looks like commons-net. I see that commons-net does have an FTPSClient class, but that is for FTPS, not SFTP.

Assuming you're talking about SFTP, I use the library JSCH.


Hi to retrieve files from ftp server I got another very powerful library which I like to share with all of you.The name of that library is edtftpj.jar Which is open source and we can easily upload and download files,we can list files and folders from particular directory of remote server.I found it very powerful. You can download it from here


Make sure you set your CLASSPATH to your JDK lib. javax.net.SocketFactory is standard in Java JDK.


You can use JSch Open Source API. Using this API you can list all files/directories from SFTP server - I also found a working example which shows how to list files from SFTP - http://kodehelp.com/get-list-files-sftp-server-java/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜