tags:

views:

151

answers:

4

Hi folks, 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) {
            // 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.

A: 

That means, you are missing a jar file here. Do you have a reference to jsse.jar?

See here: http://www.phacai.com/jar-for-javax-net

limc
Hi limc,I already added that jar but still the same problem I am facing....
Rupeshit
A: 

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

The Elite Gentleman
Yep it is properly set but still it is not giving any value.....
Rupeshit
Try compiling and runing using `javac/java -cp <classpath of libs here>`.
The Elite Gentleman
A: 

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 JSCH.

BradTrim
Thank you The Elite Gentleman, but I also made a mistake in the JSCH site. Please change the link to http://www.jcraft.com/jsch
BradTrim
Thanks guys, but anyone know public ftps and sftp test server other than chilka so that I can test the things.
Rupeshit
If you are using windows, try using FileZilla server for FTPS. I don't know what other servers are available.You can test SFTP using most openssh servers if you have shell access.http://filezilla-project.org/
BradTrim
+1  A: 

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

Rupeshit
edtFTPj/PRO is a commercial extension of edtFTPj that supports SFTP: see http://www.enterprisedt.com/products/edtftpjssl/overview.html
Bruce Blackshaw