使用Spring Integration计划通过FTP进行远程文件下载并处理内存中的文件 [英] Schedule remote file download over FTP and process file in memory with Spring Integration
问题描述
我每天需要从远程FTP服务器下载文件,并将其内容作为InputStream(或至少作为byte [])提供给类,以进行进一步处理.理想情况下,我还应该避免任何磁盘写操作.
I need to download a file from a remote FTP server every day and provide its contents to a class as an InputStream (or at least as byte[]) for further processing. Ideally I should also avoid any disk write.
任何人都可以就如何使用XML或基于注释的配置提供一些建议吗?
Can anyone give some advice on how to configure it with an XML or an annotation based configuration?
推荐答案
Spring Integration当前没有预先配置的适配器来流式传输"文件.但是,它确实具有支持此类访问的基础组件( FtpRemoteFileTemplate
).
Spring Integration currently doesn't have a pre-configured adapter to 'stream' a file; it does, however, have an underlying component (FtpRemoteFileTemplate
) that enables such access.
您可以将远程文件模板配置为Bean(使用XML或Java Config)-为它提供会话工厂等,并调用 get()
方法之一:
You can configure the remote file template as a bean (using XML or Java Config) - giving it a session factory etc, and invoke one of the get()
methods:
/**
* Retrieve a remote file as an InputStream.
*
* @param remotePath The remote path to the file.
* @param callback the callback.
* @return true if the operation was successful.
*/
boolean get(String remotePath, InputStreamCallback callback);
/**
* Retrieve a remote file as an InputStream, based on information in a message.
*
* @param message The message which will be evaluated to generate the remote path.
* @param callback the callback.
* @return true if the operation was successful.
*/
boolean get(Message<?> message, InputStreamCallback callback);
类似这样的事情...
Something like this...
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
boolean success = template.get("foo.txt", new InputStreamCallback() {
@Override
public void doWithInputStream(InputStream stream) throws IOException {
FileCopyUtils.copy(stream, baos);
}
});
if (success) {
byte[] bytes = baos.toByteArray());
...
}
或者您可以在 doWithInputStream()
中将输入流直接传递到处理程序中.
Or you can pass the input stream directly into your handler in doWithInputStream()
.
Spring Integration 3.0中添加了 FtpRemoteFileTemplate
(但采用字符串而不是 Message<?>
的 get()
变体>已在4.0中添加.)
The FtpRemoteFileTemplate
was added in Spring Integration 3.0 (but the get()
variant that takes a String rather than a Message<?>
was added in 4.0).
SftpRemoteFileTemplate
也可用.
这篇关于使用Spring Integration计划通过FTP进行远程文件下载并处理内存中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!