• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to Check whether the file in the Ftpserver is a directory or not?

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ...
reply
    Bookmark Topic Watch Topic
  • New Topic