Hi.
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:
[code=java]
System.out.prin tln("YOU ARE IN UPLOAD");
System.out.prin tln("THIS IS THE HOST: " + host);
SshClient ssh = new SshClient();
ssh.connect(hos t, 22);
//Authenticate
PasswordAuthent icationClient passwordAuthent icationClient = new PasswordAuthent icationClient() ;
passwordAuthent icationClient.s etUsername(user );
passwordAuthent icationClient.s etPassword(pass word);
System.out.prin tln("upload user and password: " + user + password);
int result = ssh.authenticat e(passwordAuthe nticationClient );
if(result != AuthenticationP rotocolState.CO MPLETE){
System.out.prin tln("Login to " + host + ":" + " " + user + "/" + password + " failed");
}
//Open the SFTP channel
SftpClient client = ssh.openSftpCli ent();
//Send the file
//Debugging Print Statements
System.out.prin tln("UPLOAD LOCALEFILE IS: " + localeFile);
remoteDirFile = "/sbt/prod/infra/run_dir/tmp/nancy";
System.out.prin tln("UPLOAD REMOTEDIRFILE IS: " + remoteDirFile);
File devstorDir=new File(remoteDirF ile);
boolean exists = devstorDir.exis ts();
if (!exists) {
// It returns false if File or directory does not exist
System.out.prin tln("the file or directory you are searching does not exist : " + exists);
System.out.prin tln("DEVSTORDIR is: " + devstorDir);
boolean successCreating Dir = devstorDir.mkdi r();
if (!successCreati ngDir) {
// Directory creation failed
System.out.prin tln("DIRECTORY: " + remoteDirFile + " WAS NOT CREATED: " + successCreating Dir);
}
else{
System.out.prin tln("GREAT SUCCESS IN CREATING A DIRECTORY: " + successCreating Dir);
System.out.prin tln("YOU ARE AT THE PUT NOW");
client.put(loca leFile, remoteDirFile);
}
}else{
// It returns true if File or directory exists
System.out.prin tln("the file or directory you are searching does exist : " + exists);
System.out.prin tln("DEVSTORDIR is: " + devstorDir);
System.out.prin tln("YOU ARE AT THE PUT NOW");
client.put(loca leFile, remoteDirFile);
}
//disconnect
client.quit();
ssh.disconnect( );
[/code]
I test the code by creating and deleting the directory on my test machine and then running the program and on each occasion, if the directory does or does not exist, I still get a false, saying that directory does not exist and it does not create a new directory.
Since i am doing this inside of an FTP, does the mkdir() and the exists() methods not work or something?
Thanks for the help!!!
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:
[code=java]
System.out.prin tln("YOU ARE IN UPLOAD");
System.out.prin tln("THIS IS THE HOST: " + host);
SshClient ssh = new SshClient();
ssh.connect(hos t, 22);
//Authenticate
PasswordAuthent icationClient passwordAuthent icationClient = new PasswordAuthent icationClient() ;
passwordAuthent icationClient.s etUsername(user );
passwordAuthent icationClient.s etPassword(pass word);
System.out.prin tln("upload user and password: " + user + password);
int result = ssh.authenticat e(passwordAuthe nticationClient );
if(result != AuthenticationP rotocolState.CO MPLETE){
System.out.prin tln("Login to " + host + ":" + " " + user + "/" + password + " failed");
}
//Open the SFTP channel
SftpClient client = ssh.openSftpCli ent();
//Send the file
//Debugging Print Statements
System.out.prin tln("UPLOAD LOCALEFILE IS: " + localeFile);
remoteDirFile = "/sbt/prod/infra/run_dir/tmp/nancy";
System.out.prin tln("UPLOAD REMOTEDIRFILE IS: " + remoteDirFile);
File devstorDir=new File(remoteDirF ile);
boolean exists = devstorDir.exis ts();
if (!exists) {
// It returns false if File or directory does not exist
System.out.prin tln("the file or directory you are searching does not exist : " + exists);
System.out.prin tln("DEVSTORDIR is: " + devstorDir);
boolean successCreating Dir = devstorDir.mkdi r();
if (!successCreati ngDir) {
// Directory creation failed
System.out.prin tln("DIRECTORY: " + remoteDirFile + " WAS NOT CREATED: " + successCreating Dir);
}
else{
System.out.prin tln("GREAT SUCCESS IN CREATING A DIRECTORY: " + successCreating Dir);
System.out.prin tln("YOU ARE AT THE PUT NOW");
client.put(loca leFile, remoteDirFile);
}
}else{
// It returns true if File or directory exists
System.out.prin tln("the file or directory you are searching does exist : " + exists);
System.out.prin tln("DEVSTORDIR is: " + devstorDir);
System.out.prin tln("YOU ARE AT THE PUT NOW");
client.put(loca leFile, remoteDirFile);
}
//disconnect
client.quit();
ssh.disconnect( );
[/code]
I test the code by creating and deleting the directory on my test machine and then running the program and on each occasion, if the directory does or does not exist, I still get a false, saying that directory does not exist and it does not create a new directory.
Since i am doing this inside of an FTP, does the mkdir() and the exists() methods not work or something?
Thanks for the help!!!
Comment