Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a java program using jsch which will connect to one unix server and display the number of files in it. Now I need to connect to another unix server and copy one file from it to first server.

code below package unix;

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Vector; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelExec; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpException;

public class DemoSftp {

public static void main(String[] args) throws JSchException, SftpException, IOException {

    String hostname = "hostname";
    String login = "login";
    String password = "password";
    String directory = "x/y/z";

    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");

    JSch ssh = new JSch();
    Session session = ssh.getSession(login, hostname, 22);
    session.setConfig(config);
    session.setPassword(password);
    session.connect();

    Channel channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp sftp = (ChannelSftp) channel;
    sftp.cd(directory);
    Vector files = sftp.ls("*");
    System.out.printf("Found %d files in dir %s%n", files.size(), directory);
  channel.disconnect();
    session.disconnect();

}

}

in second server also, I need to give user name and password. Could you please help me?

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.