I am writing a java program in which I want to ftp a file to another unix box.
First I have to check if the directory exists in which I am ftping into and if it does not exist, I have to create it:
this is the code that I am using that is not working properly:
Expand|Select|Wrap|Line Numbers
- System.out.println("YOU ARE IN UPLOAD");
- System.out.println("THIS IS THE HOST: " + host);
- SshClient ssh = new SshClient();
- ssh.connect(host, 22);
- //Authenticate
- PasswordAuthenticationClient passwordAuthenticationClient = new PasswordAuthenticationClient();
- passwordAuthenticationClient.setUsername(user);
- passwordAuthenticationClient.setPassword(password);
- System.out.println("upload user and password: " + user + password);
- int result = ssh.authenticate(passwordAuthenticationClient);
- if(result != AuthenticationProtocolState.COMPLETE){
- System.out.println("Login to " + host + ":" + " " + user + "/" + password + " failed");
- }
- //Open the SFTP channel
- SftpClient client = ssh.openSftpClient();
- //Send the file
- //Debugging Print Statements
- System.out.println("UPLOAD LOCALEFILE IS: " + localeFile);
- remoteDirFile = "/sbt/prod/infra/run_dir/tmp/nancy";
- System.out.println("UPLOAD REMOTEDIRFILE IS: " + remoteDirFile);
- File devstorDir=new File(remoteDirFile);
- boolean exists = devstorDir.exists();
- if (!exists) {
- // It returns false if File or directory does not exist
- System.out.println("the file or directory you are searching does not exist : " + exists);
- System.out.println("DEVSTORDIR is: " + devstorDir);
- boolean successCreatingDir = devstorDir.mkdir();
- if (!successCreatingDir) {
- // Directory creation failed
- System.out.println("DIRECTORY: " + remoteDirFile + " WAS NOT CREATED: " + successCreatingDir);
- }
- else{
- System.out.println("GREAT SUCCESS IN CREATING A DIRECTORY: " + successCreatingDir);
- System.out.println("YOU ARE AT THE PUT NOW");
- client.put(localeFile, remoteDirFile);
- }
- }else{
- // It returns true if File or directory exists
- System.out.println("the file or directory you are searching does exist : " + exists);
- System.out.println("DEVSTORDIR is: " + devstorDir);
- System.out.println("YOU ARE AT THE PUT NOW");
- client.put(localeFile, remoteDirFile);
- }
- //disconnect
- client.quit();
- ssh.disconnect();
Since i am doing this inside of an FTP, does the mkdir() and the exists() methods not work or something?
Thanks for the help!!!