Reputation: 61
I have to get the list of files or folder present in ftp server .I have tried the following code but I do not get the list. If I am running the same code I am getting list of file if I am making my local system as server but when I apply the same code then I am sucessfully logged in and list method of FTPClient
class is working but listFiles()
,listNames()
are not working .
import java.io.IOException;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.io.FileUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
/**
*
* @author admin
*/
public class ListFileOfServerCheck {
public static void main(String args[]) throws SocketException, IOException{
FTPClient client = new FTPClient();
ArrayList dirList=new ArrayList();
client.connect("myserver");
client.login("vishal", "vishal");
if(client.isConnected())
System.out.println("connected");
int i=0;
System.out.println("number of files on server="+client.list());
String[] names = null;
int itration=1;
while(names==null){
System.out.println(itration +"th itration");
names = client.listNames();
itration++;
}
client.logout();
client.disconnect();
}
}
Upvotes: 0
Views: 2194
Reputation: 17898
It's question what is your current dir when you log in to the FTP. Try to use:
client.listNames("/path/to/the/folder");
instead of relying on the current working directory.
It would be also good practice to check what returns the
client.login(...)
Upvotes: 1