Jbisgood9999999
Jbisgood9999999

Reputation: 253

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

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?

Upvotes: 8

Views: 31047

Answers (3)

RamVal
RamVal

Reputation: 1

    try {
            ChannelSftp channelSftp = (ChannelSftp) this.session.openChannel("sftp");
            channelSftp.connect();              
            logger.info("Home directory: {}",channelSftp.getHome());
            logger.info("Go to directory: {}",pathSFTP);
            logger.info("Move files to directory: {} ",destinationPath);
            
            channelSftp.cd(pathSFTP);
            
            List<ChannelSftp.LsEntry> filesFound = channelSftp.ls(fileExtension);
            
            logger.info("Files [{}] found ",filesFound.size());
            
            //Move to path processed
            for (LsEntry lsEntry : filesFound) {
                String pathDes = destinationPath+"/"+lsEntry.getFilename();
                channelSftp.rename(lsEntry.getFilename(), pathDes);
                logger.info("Moved to {} ",pathDes);
            }
            
            //Close Connection
            channelSftp.exit();
            channelSftp.disconnect();
            
            logger.info("All files has been downloaded successfully");
            
        } catch (JSchException | SftpException e) {
            logger.error("Error move files directory [{}]",destinationPath);
            logger.error("An error has occurred while Move files {}", e.getMessage());
            e.printStackTrace();
        }
        }

Upvotes: 0

Jbisgood9999999
Jbisgood9999999

Reputation: 253

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 );
}

Upvotes: 10

Kushal Shukla
Kushal Shukla

Reputation: 110

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

Upvotes: -1

Related Questions