文件转换器
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 扩展自 AbstractFilePayloadTransformer,将 File 对象转换为 String。
若无其他用途,此功能可用于调试(建议与 线卡监听器 配合使用)。
要配置特定文件的转换器,您可以使用来自 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选项向转换器发出信号,指示它在转换完成后删除传入文件。
这绝不能替代在多线程环境(例如在使用 Spring Integration 时)中使用AcceptOnceFileListFilter和FileReadingMessageSource的情况。