Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
4.2k views
in Java FTP by
I have got a remote directory called ftpPath,Is there someway I can know is it a file or directory directly through ftpPath,create a FTPFile object?
Thanks a lot !

4 Answers

0 votes
by (162k points)
If you list the parent directory using dirDetails(), that should tell you.
0 votes
by
Thanks for your answer.But I just don't know if it is a file or a directory on the remote server.
what I can only get is a path, like a method like this boolean fileOrDirectroy(String ftppath), true for file, false for directory
Any suggestion will be highly appreciated
0 votes
by (162k points)
It isn't necessarily an easy thing to figure out.

The best approach is to use dirDetails() on the directory the file (or dir) is in, search through the FTPFile objects that are returned until you match the name, and see if it is a directory.
0 votes
by
Thanks a lot. I have figured it out myself! Here is the code,I hope it will be helpful for someone !
public boolean fileOrDirectory(String remotePath){
    if (remotePath == null || remotePath.equals("") ){
          throw new Exception("Input is null"); 
    }
    else{
          remotePath = remotePath.trim.replace("\\","/");
          String path = null;
          if(remotePath.lastIndexOf("/") == -1){
              path ="..";
          }
          else{
              path = remotePath.substring(0,filename.lastIndexOf("/"));   
          }
          String name = remotePath.substring(filename.lastIndexOf("/")+1,filename.length());
          ftp.changeDirectory(path);
          FTPFile[] fileInfo = ftp.directoryList();
          for(int i = 0; i< fileInfo.length; i++){
                FTPFile file = fileInfo[i];
                if(file.getName().equals(name)){
          if (file.isFile()){
                             return true;
                       }               
               else{
                             return false;
                       }
                 }
         }
         throw new Exception("Can not find it on the server"+remotePath);
}

Categories

...