check if a directory exists using FTPClient
-
12-12-2019 - |
Question
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
Solution
You want the FTP STAT command.
You can use this API call to check what you're after.
OTHER TIPS
Try this method:
public static boolean isExists(FTPClient ftpClient, String pathName)
{
ftpClient.getStatus(pathName);
return FTPReply.isPositiveCompletion(ftpClient.getReplyCode());
}
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***
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow