|
如需使用最新稳定版本,请使用 Spring Integration 7.0.4! |
文件转换器
To transform data read from the file system to objects and the other way around, you need to do some work.
Unlike FileReadingMessageSource and to a lesser extent FileWritingMessageHandler, you probably need your own mechanism to get the job done.
For this, you can implement the Transformer interface.
Alternatively, you can extend the AbstractFilePayloadTransformer for inbound messages.
Spring Integration provides some obvious implementations.
查看 Javadoc for the Transformer interface 以了解哪些 Spring Integration 类实现了它。
同样,您可以检查 Javadoc for the AbstractFilePayloadTransformer class 以了解哪些 Spring Integration 类扩展了它。
FileToByteArrayTransformer 继承自 AbstractFilePayloadTransformer,并通过 Spring 的 FileCopyUtils 将 File 对象转换为 byte[]。
通常使用一系列转换器比将所有转换逻辑放在单个类中更好。
在这种情况下,从 File 到 byte[] 的转换可能是一个逻辑上的第一步。
FileToStringTransformer extends AbstractFilePayloadTransformer 将 File 对象转换为 String。
除此之外,这在调试时可能非常有用(建议与 线卡(wire tap) 配合使用)。
要配置特定文件的转换器,您可以使用来自 file 命名空间的相应元素,如下例所示:
<int-file:file-to-bytes-transformer input-channel="input" output-channel="output"
delete-files="true"/>
<int-file:file-to-string-transformer input-channel="input" output-channel="output"
delete-files="true" charset="UTF-8"/>
delete-files选项向转换器发出信号,指示它在转换完成后删除传入文件。
这绝不能替代在FileReadingMessageSource被用于多线程环境(例如当您通常使用Spring Integration时)时使用AcceptOnceFileListFilter。