I am getting inputstream from the Jsch channelSFTP like below.
ChannelSftp channelSftp = (ChannelSftp) channel;
InputStream input = channelsftp.get(unixPath); // unixPath is path to my file which is on SFTP server
I have to attach the file in the unixPath
in the Spring JavaMail attachment. But when I see API of Spring JavaMail addAttachment()
method it takes only InputStreamSource
or Datasource
.
My problem is I am not able to get the InputStreamSource
or Datasource
from the inputStream which I am getting form SFTP channel.
How Can I get InputStreamSource
or Datasource
from the above InputStream
?
To convert an InputStream Object int to a String using this method. Instantiate the Scanner class by passing your InputStream object as parameter. Read each line from this Scanner using the nextLine() method and append it to a StringBuffer object. Finally convert the StringBuffer to String using the toString() method.
public class InputStreamResource extends AbstractResource. Resource implementation for a given InputStream . Should only be used if no other specific Resource implementation is applicable. In particular, prefer ByteArrayResource or any of the file-based Resource implementations where possible.
From the documentation, InputStreamSource
is an interface. One of its implementations is InputStreamResource
, which has a constructor that takes in an InputStream
. Here is the JavaDoc for it.
You should be able to setup your call as such:
addAttachment("Not porn", new InputStreamResource(inputStream));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With