| Author |
How to Check whether the file in the Ftpserver is a directory or not?
|
yathish Gatty
Ranch Hand
Joined: Oct 05, 2004
Posts: 31
|
|
Hi, I am trying to ftp a ftp server using the ftpclient library 'ameobacode'.I am able to get the names of files and directory on the remote server.But I am not able to determine whether it is a directory or not. The names of files is Array of string. Please suggest a method to determine this.
|
 |
Stephen Huey
Ranch Hand
Joined: Jul 15, 2003
Posts: 618
|
|
I took a look at the API: http://www.amoebacode.com/ftp/javadoc/com/amoebacode/ftp/FTPClient.html Unless my eyes are foggy, I don't see a way to do it (unless you want to try changing directories to everyone of the names in the file list and see if it lets you go there). I use the Apache Commons FTPClient, and you can do it with that: http://jakarta.apache.org/commons/net/apidocs/org/apache/commons/net/ftp/FTPClient.html Just call their listFiles() method, and you'll get an array of FTPFile objects. The FTPFile class has the method isDirectory() as you can see here: http://jakarta.apache.org/commons/net/apidocs/org/apache/commons/net/ftp/FTPFile.html
|
 |
yathish Gatty
Ranch Hand
Joined: Oct 05, 2004
Posts: 31
|
|
Thanks for the help Stephen. I tried using commons.net library.But I got exceptions.Please help me with this.The code I have written is below. import org.apache.commons.net.ftp.*; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; public class MyFTP { public static void main(String[] args) { String user="user-name"; String psw="password"; String server="134.238.198.12"; String directory="/lic"; try { FTPClient f= new FTPClient(); f.connect(server); f.login(user, psw); System.out.print(f.getReplyString()); /************* this does not work *************/ FTPFile[] files = f.listFiles(); for(int i=0;i< files.length;i++){ System.out.println("files:" + files[i].getName()); }//end of for loop /************* up to this *************/ System.out.println(); f.disconnect(); }catch(Exception e){ System.out.println("Error:"+e); } }//end of function } } These are the exception I got: java.lang.NoClassDefFoundError: org/apache/oro/text/regex/MalformedPatternException at org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory.createUnixFTPEntryParser(DefaultFTPFileEntryParserFactory.java:121) at org.apache.commons.net.ftp.parser.DefaultFTPFileEntryParserFactory.createFileEntryParser(DefaultFTPFileEntryParserFactory.java:84) at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:2306) at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2055) at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2106) at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2146) at MyFTP.main(MyFTP.java:23) Exception in thread "main" Thank you.
|
 |
Christian Schnobeladish
Greenhorn
Joined: Nov 08, 2004
Posts: 3
|
|
Hi Greenhorn ! I received the same error as you. There is a missing library in your project: the Jakarta Oro library. You can download it on the Jakarta web site. I added it and I hava now another stacktrace during the listFile execution : java.io.IOException: Host attempting data connection 10.10.1.131 is not same as server 10.10.1.130 at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:508) at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:2335) at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:2309) at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2055) at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2106) at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2146) If somebody has any idea about how to correct it ...
|
 |
 |
|
|
subject: How to Check whether the file in the Ftpserver is a directory or not?
|
|
|