21

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?

Lii
  • 11,553
  • 8
  • 64
  • 88
SRy
  • 2,901
  • 8
  • 36
  • 57

2 Answers2

50

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));
nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
0

I host a Spring Boot application in Docker on my VPS. This code will work on a local system whilst developing in an IDE as well as if the .JAR-file is wrapped in a Docker image:

Example case:

I like to get a PDF. The PDf document is located at static/docs/not-a-porn.PDF.

1.) Wire the class ResourceLoader.

private final ResourceLoader resourceLoader;

public MyService(@Autowired ResourceLoader resourceLoader) {  
    this.resourceLoader = resourceLoader;
}

2.) Create a resource by pathOfAttachment (String).

String pathOfAttachment = "classpath:static/docs/not-a-porn.PDF"
Resource resource = this.resourceLoader.getResource(pathOfAttachment);

Resource is an interface that extends InputStreamSource as you can see in the documentation:

public interface Resource extends InputStreamSource

Source: org.springframework.core.io Link to documentation (click here)

Kevin O.
  • 355
  • 3
  • 11