user5843610
user5843610

Reputation:

Java File Transfer using JSch

I have a Java SFTP Transfer application working fine using a file that is saved on disk. I like to change the logic to use a string or stream. All the information I can find is that it only takes FileInputStream. Any help would be great.

ChannelSftp c = createSession();
try {
    File f = new File(workingFile);
    c.put(new FileInputStream(f), f.getName());
} catch (Exception e) {
    System.err.println("Storing remote file failed. "+e.toString());
    throw e;
}

Upvotes: 0

Views: 1521

Answers (1)

Voicu
Voicu

Reputation: 17860

Based on the JSch 0.1.44 (unofficial) Javadoc maintained by Paŭlo Ebermann found here, ChannelSftp has several overloaded put(...) methods. So you can provide an InputStream (or any subclass), not just a FileInputStream reference. For example, put(InputStream src, String dst) would upload a file from an input stream src to the dst, which is the remote destination file name, relative to the current remote directory.

You can also provide the String path to the file you want transferred. For example, the put(String src, String dst) method with the parameters:

  • src - the local source file name, absolute or relative to the current local directory.
  • dst - the remote destination file name, absolute or relative to the current remote directory.

Upvotes: 1

Related Questions