Search code examples
javaspringspring-integrationspring-integration-sftp

Writing to a remote file using Spring Integrations Sftp Streaming java configuration


How to write to a remote file using Spring integrations Sftp Streaming .I got some code using xml but I have to strictly use java configuration and I cant find any . I have to keep on appending some data to the file after some validation failure.So its not a one time write/transfer but I have to maintain the connection with remote and keep on appending the file with error logs.Any help appreciated.


Solution

  • Use an SftpRemoteFileTemplate execute() with a SessionCallback ...

    SftpRemoteFileTemplate template = new SftpRemoteFileTemplate(sessionFactory);
    PipedInputStream pipe = new PipedInputStream();
    OutputStream outputStream = new PipedOutputStream(pipe);
    template.execute(s -> {
        s.write(pipe, "/foo/bar.log");
        return null;
    });
    

    Writing to the output stream (from another thread) will be piped to the input stream. Transfer will end when the stream is closed.