I have to invoke unix commands from Java and I have to use ProcessBuilder. I want to login through SFTP using key authentication and I want to SFTP the file from local directory to remote location.
ProcessBuilder pb = new ProcessBuilder("sftp","-oIdentityFile=privateKey","-b","commands.txt","username@hostname");
Content in commands.txt is
put localFileDirectory remoteDirectory
exit
Is there any way that I can give localFileDirectory and remoteDirectory in processbuilder? Those should be dynamic and I have to give it processbuilder.
Two possible approaches:
Use the
scp
command instead. It does the same ssh-based file transfer, but allows you to specify source and destination on the command line.The
-r
is for "recursive", needed if you are transferring a whole folder. It's not needed if you're just transferring a single file.Generate the
commands.txt
file from your Java code.