Hi,
When I use FTPClient.exists(FileName) instead of returning false if a file does not exist, FTPClient throws FTPException. Here is a sample code:
public class Test {
private static Logger log = Logger.getLogger(Test.class);
private static FTPClient ftp;
public static void main(String[] args) throws Exception{
Logger.setLevel(Level.ALL);
ftp = new FTPClient();
FTPMessageCollector listener = new FTPMessageCollector();
ftp.setMessageListener(listener);
try {
ftp.setRemoteHost("x.x.x.x");
ftp.setTimeout(61*1000);
ftp.connect();
ftp.login("xxx", "xxx");
ftp.setConnectMode(FTPConnectMode.PASV);
ftp.setType(FTPTransferType.BINARY);
File file = new File("D:\\programs\\test.exe");
// This file SHOULD NOT EXIST ON THE SERVER
if(ftp.exists(file.getName())==true){
log.info("Oh No - File exist");
}
// Upload the file
log.info("Uploading File");
ftp.put(file.toString(), file.getName());
ftp.quit();
} catch (IOException e) {
System.out.println("Main - IO Exception");
e.printStackTrace();
} catch (FTPException e) {
System.out.println("Main - FTP Exception");
e.printStackTrace();
}
}
}
Here is the DEBUG Output:
DEBUG [FTPClient] 28 Jun 2007 14:19:13.147 : Connecting to /x.x.x.x:21
DEBUG [SocketUtils] 28 Jun 2007 14:19:13.257 : Invoking connect with timeout=61000
DEBUG [FTPControlSocket] 28 Jun 2007 14:19:13.377 : 220 (vsFTPd 2.0.5)
DEBUG [FTPControlSocket] 28 Jun 2007 14:19:13.397 : ---> USER xxx
DEBUG [FTPControlSocket] 28 Jun 2007 14:19:13.417 : 331 Please specify the password.
DEBUG [FTPControlSocket] 28 Jun 2007 14:19:13.417 : ---> PASS ***
DEBUG [FTPControlSocket] 28 Jun 2007 14:19:13.457 : 230 Login successful.
DEBUG [FTPControlSocket] 28 Jun 2007 14:19:13.467 : ---> TYPE I
DEBUG [FTPControlSocket] 28 Jun 2007 14:19:13.477 : 200 Switching to Binary mode.
DEBUG [FTPControlSocket] 28 Jun 2007 14:19:13.477 : ---> SIZE test.exe
DEBUG [FTPControlSocket] 28 Jun 2007 14:19:13.527 : 550 Could not get file size.
DEBUG [FTPClient] 28 Jun 2007 14:19:13.527 : SIZE not supported - trying MDTM
DEBUG [FTPControlSocket] 28 Jun 2007 14:19:13.527 : ---> MDTM test.exe
DEBUG [FTPControlSocket] 28 Jun 2007 14:19:13.547 : 550 Could not get file modification time.
DEBUG [FTPClient] 28 Jun 2007 14:19:13.547 : MDTM not supported - trying RETR
DEBUG [FTPControlSocket] 28 Jun 2007 14:19:13.717 : ---> PORT 192,168,254,101,4,218
DEBUG [FTPControlSocket] 28 Jun 2007 14:19:13.727 : 200 PORT command successful. Consider using PASV.
DEBUG [FTPControlSocket] 28 Jun 2007 14:19:13.727 : ---> RETR test.exe
DEBUG [FTPControlSocket] 28 Jun 2007 14:19:13.747 : 550 Failed to open file.
WARN [FTPClient] 28 Jun 2007 14:19:13.747 : Unable to determine if file 'test.exe' exists.
Main - FTP Exception
com.enterprisedt.net.ftp.FTPException: Unable to determine if file 'test' exists.
at com.enterprisedt.net.ftp.FTPClient.exists(FTPClient.java:1473)
at Test.main(Test.java:52)
Thanks for helping!