File Transformers

要将从文件系统读取的数据转换为对象,反之亦然,您需要执行一些工作。不像 FileReadingMessageSource 和较低程度的 FileWritingMessageHandler,您可能需要自己的机制来完成工作。为此,您可以实现 Transformer 接口。或者,您可以扩展 AbstractFilePayloadTransformer 以获取入站消息。Spring Integration 提供了一些明显的实现。

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 类扩展了它。

See the Javadoc for the Transformer interface to see which Spring Integration classes implement it. Similarly, you can check the Javadoc for the AbstractFilePayloadTransformer class to see which Spring Integration classes extend it.

FileToByteArrayTransformer 扩展了 AbstractFilePayloadTransformer,并使用 Spring 的 FileCopyUtilsFile 对象转换为 byte[]。使用转换器序列往往比将所有转换都放在一个类中更好。在这种情况下,将 File 转换为 byte[] 可能是一个合乎逻辑的第一步。

FileToByteArrayTransformer extends AbstractFilePayloadTransformer and transforms a File object into a byte[] by using Spring’s FileCopyUtils. It is often better to use a sequence of transformers than to put all transformations in a single class. In that case the File to byte[] conversion might be a logical first step.

FileToStringTransformer 扩展了 AbstractFilePayloadTransformer,将 File 对象转换为 String。无论如何,这对于调试很有用(考虑将其与 wire tap 一起使用)。

FileToStringTransformer extends AbstractFilePayloadTransformer convert a File object to a String. If nothing else, this can be useful for debugging (consider using it with a wire tap).

要配置特定于文件的转换器,您可以使用文件命名空间中的适当元素,如下例所示:

To configure file-specific transformers, you can use the appropriate elements from the file namespace, as the following example shows:

<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 的方法。

The delete-files option signals to the transformer that it should delete the inbound file after the transformation is complete. This is in no way a replacement for using an AcceptOnceFileListFilter when the FileReadingMessageSource is being used in a multi-threaded environment (such as when you use Spring Integration in general).