Writing to a remote file using Spring Integrations

2019-09-14 07:22发布

问题:

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.

回答1:

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.