0

When i am trying to move file from fileDirectory1 to fileDirectory2 .. Is there any way to move or copy that file from one path to another in one FTP server.Please anyone can help me.

Here is my sample code:

                      String existingfile = file.getFilename();
                        String newfile =file.getFilename();
                        String fileDirectory1 = clients.getFtpFolder() + "/" + "unprocessed" + "/";
                        String fileDirectory2 = clients.getFtpFolder() + "/" + "processed" + "/";
                        sftpChannel.cd(fileDirectory1);
                        if (sftpChannel.get(newfile) != null){
                            sftpChannel.rename(fileDirectory1 + newfile , 
                                    fileDirectory2 + newfile );
                            sftpChannel.cd(fileDirectory2);
                            sftpChannel.rm(existingfile );
                        }

Console:

Caused by:2: No such file
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2846)

I have tried How to move file from directory A to directory B in remote server?

Move a directory in remote server to another location in the same remote server using jsch

Community
  • 1
  • 1
Sitansu
  • 3,225
  • 8
  • 34
  • 61

1 Answers1

1

You can try ChannelSftp#rename:

sftpChannel.rename(oldPath, newPath);

Note that oldPath and newPath can be a relative path to your current remote working directory or a absolute remote path.

Edited:

Seems you use sftpChannel.cd(fileDirectory1) to change your working directory to fileDirectory1 and suppose newfile is inside that directory, you can have a try with something like

sftpChannel.rename(filename, destFilePath);

Note that filename is a file name and destFilePath is the remote file path of the destination. I have used it before and it works fine.

You code will be something like:

sftpChannel.rename(newfile, fileDirectory2 + newfile); 

in your code. Hope this can help

Wilson
  • 11,339
  • 2
  • 29
  • 33
  • http://stackoverflow.com/questions/13277218/move-a-directory-in-remote-server-to-another-location-in-the-same-remote-server – Sitansu Jul 06 '16 at 07:35
  • not work means exception occurs? the file do not move to remote path? or something other else? – Wilson Jul 06 '16 at 07:38
  • 1
    Please have a try with my updated answer see whether can help – Wilson Jul 06 '16 at 07:47