How can I use SSHJ to stream a file over SCP?

I am using SSHJ library ( ).

I can use sshClient.newSCPFileTransfer();

to get my object SCPClient

, but then the call

requires either String as the path to the target file, or the LocalDestFile object. All I want is to open a stream to download the file. Is there a way to do this using SSHJ?

I looked at various implementations of LocalDestFile, but nothing was applicable to this use case.

A specific use case is to download a remote file and transfer it to a client browser for a web application. I have to use SCP for this and hence want to use the SSHJ library.


source to share

1 answer

I was able to solve this by extending the class InMemoryDestFile

and passing the desired object into the project OutputStream

and returning it to the method getOutputStream


An example is shown in the following GIST:

You seem to need to close OutputStream

yourself when you're done, so watch out for that.



All Articles