0

I want to write a String generated by a POJO to be written to a file in a remote server accessible only through scp/ssh. Would like to avoid creating local temporary file and scp the files to the remote machines. Implementation using Jsch library is preferred.

rogue-one
  • 11,259
  • 7
  • 53
  • 75
  • You can write the String directly to an OutputStream, but since a String contains chars and not bytes, you must decide on an encoding (charset) to convert the String's chars to bytes. When in doubt, use `string.getBytes(StandardCharsets.UTF_8)`. – VGR Aug 06 '14 at 21:11
  • 1
    You should consider using sftp and [ChannelSftp](http://epaul.github.io/jsch-documentation/javadoc/com/jcraft/jsch/ChannelSftp.html). It has functions to open a remote file which return an `OutputStream` that you can write data to. – Kenster Aug 06 '14 at 21:13
  • @Kenster your approach looks promising, I will explore this further. – rogue-one Aug 06 '14 at 21:15

1 Answers1

1

I followed @Kenster's recommendation and ended up doing something like below.

session = jsch.getSession(user, agent, port);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
((ChannelSftp) channel).put(new ByteArrayInputStream(args[0].getBytes()), args[1]);
rogue-one
  • 11,259
  • 7
  • 53
  • 75