Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can convert inputstream to InputStreamsource or Datasource to attach in Spring JavaMail

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?

like image 336
SRy Avatar asked Nov 05 '12 18:11

SRy


People also ask

How do I change InputStream to String?

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.

What is InputStreamResource in Java?

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.


1 Answers

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));
like image 109
nicholas.hauschild Avatar answered Oct 17 '22 22:10

nicholas.hauschild