8

I am using JSch to connect to SFTP in a website which is made from GWT. I had read a little example of sftpChannel.get(), sftpChannel.rename(), sftpChannel.rm()

But I didn't find a solution that copy a file from remote server a directory to remote server b directory.

For example, I want to copy file from /appl/user/home/test/temp to /appl/user/home/test/. Filename = abc.jpg.

I stunned here for few hours since most of the solution from network is getting file from remote server to local, or uploading file from local to remote server.

String existingfile = "abc.jpg";
String newfile = "123.jpg";
FileDirectory = "/appl/user/home/test/";
sftp.cd(FileDirectory+"temp/");
sftp.rename(newfile, FileDirectory+newfile);

Let's say, abc.jpg is existing in /appl/user/home/test/

And I upload a 123.jpg in /appl/user/home/test/temp/.

Now, I want to move 123.jpg to /appl/user/home/test/ and remove abc.jpg in /appl/user/home/test/.

What should I do?

Jbisgood9999999
  • 253
  • 1
  • 5
  • 15

2 Answers2

10

It seems like SftpChannel.rename(); need to use full path of file instead of cd to the directory that the file I am going to move.

String existingfile = "abc.jpg";
String newfile = "123.jpg";
FileDirectory = "/appl/user/home/test/";
sftp.cd(FileDirectory+"temp/");
if (sftp.get( newfile ) != null){
    sftp.rename(FileDirectory + "temp/" + newfile , 
        FileDirectory + newfile );
    sftp.cd(FileDirectory);
    sftp.rm(existingfile );
}
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Jbisgood9999999
  • 253
  • 1
  • 5
  • 15
  • I have a query, is/ is not sftp.get(newFile) inside the if condition on fifth line going to download the new file into the system on which this program is running? – user3237842 Feb 05 '21 at 10:54
-1

You can write a normal Java FileInputStreamand FileOutputStreamcode and instead of using paths like these /appl/user/home/test/temp use full path with its IpAddress or remote server name + your path eg myremoteserver/appl/user/home/test/temp

Kushal Shukla
  • 110
  • 1
  • 7
  • But i don't want to make the File transfer to client side. My SFTP server located at Unix, so it is the absolute path. Isn't FileInputStream and FileOutputStream is making the file from SFTP to local (client side) ? I think i should not making the file from SFTP (Server side) to client side then to SFTP again. – Jbisgood9999999 May 05 '15 at 10:41