0
votes

I'm using FTPClient (org.​apache.​commons.​net.​ftp.​FTPClient) to upload some files to a FTP server.
I need to check if parent directories exists or not; and create them if needed.
How can I check file/directory existence using FTPClient?

Thanks

3

3 Answers

2
votes

You want the FTP STAT command.

You can use this API call to check what you're after.

1
votes

Try this method:

public static boolean isExists(FTPClient ftpClient, String pathName)
{
    ftpClient.getStatus(pathName);
    return FTPReply.isPositiveCompletion(ftpClient.getReplyCode());
}
0
votes

Try this code, it worked for me.:

FTPClient ftpClient=null;
        (ftpClient = new FTPClient()).connect("xxx.xxx.xxx.xxx");           

        if(ftpClient.login("user", "pass")){            
            System.out.println(ftpClient.cwd("folder/subfolder"));    
***// if returns 250, folder exists  and if returned 550 folder does not exist***       

        }