1

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

For example, I want to copy file from /RD-CRO-TMF/Boundary/ to /RD-CRO-TMF-PROD/Test_Load/PPD/test/.

Filename = abc.Zip.

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

package test;


public class Tesing {

private static final String USER_PROMPT = "Enter username@hostname:port";
private static final boolean USE_GUI = true;

public static void main(final String[] arg) {
    Session session = null;
    ChannelSftp channelSftp = null;
    try {
        final JSch jsch = new JSch();

        final String defaultInput = System.getProperty("user.name") + "@localhost:22";
        String input = (USE_GUI) ? JOptionPane.showInputDialog(USER_PROMPT, defaultInput)
                : System.console().readLine("%s (%s): ", USER_PROMPT, defaultInput);
        if (input == null || input.trim().length() == 0) {
            input = defaultInput;
        }
        final int indexOfAt = input.indexOf('@');
        final int indexOfColon = input.indexOf(':');
        String user = "oaz4188846";
        String password = "POG84286";
        String host = "us1salx43564.corpnet2.com";
        int port = 22;
        session = jsch.getSession(user, host, 22);

        final UserInfo ui = new MyUserInfo();
        session.setUserInfo(ui);
        System.out.println("Establishing Connection...");
        session.connect();
        System.out.println("Connection established.");
        System.out.println("Creating SFTP Channel.");
        channelSftp = (ChannelSftp) session.openChannel("sftp");
        channelSftp.connect();
        System.out.println("SFTP Channel created.");
        String newfile = "abc.zip";
        String FileDirectory = "/RD-CRO-TMF/Boundary/";
        String FileDirectory1 = "/RD-CRO-TMF-PROD/Test_Load/PPD/test/";
        channelSftp.cd(FileDirectory);
        if (channelSftp.get(newfile) != null) {
            channelSftp.rename(FileDirectory + newfile, FileDirectory1 + newfile);
            // channelSftp.cd(FileDirectory);

        }
        // channelSftp.put("RD-CRO-TMF/Boundary/abc.zip",
        // "/RD-CRO-TMF-PROD/Test_Load/PPD/test");
    } catch (JSchException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SftpException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    finally {
        if (channelSftp != null) {
            channelSftp.exit();
        }
        if (session != null) {
            session.disconnect();
        }
    }
}

public static class MyUserInfo implements UserInfo {
    private String password;

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public boolean promptYesNo(final String str) {
        final Object[] options = { "yes", "no" };
        final boolean yesNo = (USE_GUI)
                ? JOptionPane.showOptionDialog(null, str, "Warning", JOptionPane.DEFAULT_OPTION,
                        JOptionPane.WARNING_MESSAGE, null, options, options[0]) == 0
                : System.console().readLine("Enter y or n: ").equals("y");
        return yesNo;
    }

    @Override
    public String getPassphrase() {
        return null;
    }

    @Override
    public boolean promptPassphrase(final String message) {
        return true;
    }

    @Override
    public boolean promptPassword(final String message) {
        if (!USE_GUI) {
            password = new String(System.console().readPassword("Password: "));
            return true;
        } else {
            final JTextField passwordField = new JPasswordField(20);
            final Object[] ob = { passwordField };
            final int result = JOptionPane.showConfirmDialog(null, ob, message, JOptionPane.OK_CANCEL_OPTION);
            if (result == JOptionPane.OK_OPTION) {
                password = passwordField.getText();
                return true;
            } else {
                return false;
            }
        }
    }

    @Override
    public void showMessage(final String message) {
        if (!USE_GUI) {
            System.console().printf(message);
        } else {
            JOptionPane.showMessageDialog(null, message);
        }
    }
}

}

I see below exception when run

Establishing Connection...
Connection established.
Creating SFTP Channel.
SFTP Channel created.
4: Failure
    at com.jcraft.jsch.ChannelSftp.throwStatusError(Unknown Source)
at com.jcraft.jsch.ChannelSftp.rename(Unknown Source)
at test.Tesing.main(Tesing.java:69)

I saw example code from here How to move file from directory A to directory B in remote server? but not sure where to merge in my requirement. Please help

enter code here
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 );
}
  • does this username have write access to the destination directory? – Kartik Nov 01 '18 at 05:55
  • yes this user has both read/write access.Also I use same credentials in Putty – user2996028 Nov 01 '18 at 06:04
  • Thanks for the info. This doesn't look like the full error, are you able to turn on the DEBUG level of the logs? Or better, try to debug. – Kartik Nov 01 '18 at 06:15
  • 1) If you connect with any standalone (command-line or GUI) client, can you move the file as you need? 2) Do you want to "copy" or "move"? Those are two different operations and you mix them in your question. 3) *"not sure where to merge in my requirement"* - What *"requirement"*? – Martin Prikryl Nov 01 '18 at 07:16
  • Thanks for help...I'm writing this code to testing purpose. Main aim is to copy file then unzip file. For now I just want to copy file from source A to destination B both are in remote location. – user2996028 Nov 01 '18 at 07:39
  • If you want to copy, then see: [How do I transfer a file from one directory to another using Java SFTP Library JSch?](https://stackoverflow.com/q/32787456/850848) – Martin Prikryl Nov 01 '18 at 07:49
  • And you didn't answer all of my questions. – Martin Prikryl Nov 01 '18 at 07:50
  • @MartinPrikryl - The above link helped me lot. I have used below code snippet in my code but now iam facing performance issue where 10mb for file takes 5mins to transfer approx. `code ChannelSftp channelRead = (ChannelSftp)session.openChannel("sftp"); channelRead.connect(); ChannelSftp channelWrite = (ChannelSftp)session.openChannel("sftp"); channelWrite.connect(); InputStream out = null; out = sftpChannel.get("/RD-CRO-TMF/Boundary/testing.zip"); channelWrite.put(out, "/RD-CRO-TMF-PROD/Test_Load/PPD/test/testing.zip"); ` – user2996028 Nov 05 '18 at 13:12
  • Is there possible i can improve rate of transfer. Thanks for help. – user2996028 Nov 05 '18 at 13:14
  • That's for a different question. – Martin Prikryl Nov 05 '18 at 13:24
  • Though did you consider using `cp` shell command as suggested in [Using SFTP in Java, How do I transfer a file from one folder to another?](https://stackoverflow.com/q/32742066/850848)? – Martin Prikryl Nov 05 '18 at 13:25

0 Answers0