Naidu
Naidu

Reputation: 255

How to push the file content using SCP to a remote file directly from the local stream object to a remote file

How to push the file content using SCP to a remote file directly from the local stream object to a remote file, so it isn't necessary to store the file temporary on a local disk?

Currently I am creating stream locally and trying directly push to the remote server it is able to establish session successfully but channel.isConnected is getting false by which remote file is not getting created,please help.

    Runtime rt = Runtime.getRuntime();
    BufferedWriter out= new BufferedWriter(new FileWriter(f));
    Process proc = rt.exec(cmd);       
    BufferedReader stdInput = new BufferedReader(new 
         InputStreamReader(proc.getInputStream()));
    BufferedReader stdError = new BufferedReader(new 
         InputStreamReader(proc.getErrorStream()));

    //Intiate SCP connection

    JSch jsch=new JSch();
    jsch.setConfig("StrictHostKeyChecking", "no");
    Session session=jsch.getSession("userName", "scpServer", 22);        
    UserInfo ui=new MyUserInfo(); //Passing password from this class
    session.setUserInfo(ui);
    session.connect();

    String rfile="/home/npachava/SCPTest/myTest.bson";
    // exec 'scp -f rfile' remotely
    String command="scp -f "+rfile;
    Channel channel=session.openChannel("exec");
    ((ChannelExec)channel).setCommand(command);
    System.out.println("session.getServerVersion()"+session.getServerVersion());;
    System.out.println("session.isConnected() "+session.isConnected());
    System.out.println("channel.isConnected() "+channel.isConnected());
    // get I/O streams for remote scp
    OutputStream outScp=channel.getOutputStream();
    InputStream inScp=channel.getInputStream();

    channel.connect();

    // read the output from the command
    System.out.println("standard output of the command:\n");
    String s = null;
    while ((s = stdInput.readLine()) != null) {
        System.out.println(s);
        out.write(s);
        byte[] b = s.getBytes();
        System.out.println("Bytes Length"+b.length);
        outScp.write(b);



    }
    System.out.println("OutPutSCP"+outScp);
    // read any errors from the attempted command
    System.out.println("standard error of the command (if any):\n");
    while ((s = stdError.readLine()) != null) {
        System.out.println(s);
    }
    out.close();
    outScp.flush();
    outScp.close();
    channel.disconnect();
    session.disconnect();
}

I am referring the example mentioned in http://www.jcraft.com/jsch/examples/ScpTo.java.html

Upvotes: 2

Views: 4034

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202197

Just use your in-memory stream instead of the FileInputStream.

In this line (of ScpTo.java code, not your code above), assign your "local" stream object (e.g. a ByteArrayInputStream) to the fis, instead of creating FileInputStream.

fis=new FileInputStream(lfile);

Make sure you seek your stream to its beginning first.

Upvotes: 1

Related Questions