如何解决Mule文件连接器‘On New or Updated File’的outputEncoding不生效问题?
问题原因
file:listener的outputEncoding参数作用是定义读取文件内容时使用的编码,而非对移动的文件进行编码转换。你当前用listener的moveToDirectory和renameTo是直接对原文件做移动/重命名操作,不会修改文件内容的编码,所以设置这个参数根本不会生效。
解决方法
要实现编码转换,需要先读取文件内容、转码后再写入新文件,不能依赖listener的直接移动功能。修改后的流配置如下:
<?xml version="1.0" encoding="UTF-8"?> <file:config name="File_Config" doc:name="File Config" doc:id="b26e348c-074d-4fec-8af8-6b0952cfb5ec" defaultWriteEncoding="ISO-8859-1" > <file:connection/> </file:config> <configuration-properties doc:name="Configuration properties" doc:id="e3ba4c3e-0ccd-446f-a844-991c64be0318" file="gross.properties" /> <flow name="sunnyftpFlow" doc:id="1c20d716-1aa4-4fba-b15e-f38f461273d6"> <!-- 仅监听文件,不直接移动 --> <file:listener doc:name="On New or Updated File" doc:id="53c0f78c-5f91-47ce-a32b-49433b4a49fd" directory="C:\FTP\SUNNYFTP" config-ref="File_Config" outputMimeType="text/flatfile"> <scheduling-strategy> <fixed-frequency/> </scheduling-strategy> </file:listener> <!-- 读取原文件内容,指定原文件编码为UTF-8(根据实际情况调整) --> <file:read doc:name="Read File" path="#[message.attributes.path]" outputEncoding="UTF-8"/> <!-- 写入转码后的文件到目标目录,设置新文件名和目标编码 --> <file:write doc:name="Write Transcoded File" path="C:\FTP\TMP\#[ 'TEST_'++ now() as DateTime as String {format: 'yyyyMMddHHmmss'} ++ '_'++ message.attributes.fileName]" outputEncoding="ISO-8859-1"/> <!-- 删除原文件 --> <file:delete doc:name="Delete Original File" path="#[message.attributes.path]"/> <logger level="INFO" doc:name="Logger" message="文件已完成转码、重命名和移动"/> </flow> </mule>
关键说明
file:read的outputEncoding要设置为原文件的编码(比如你的原文件是UTF-8就设这个值),确保正确读取内容file:write的outputEncoding设置为你需要的ISO-8859-1,完成编码转换- 最后用
file:delete移除原文件,模拟“移动”的效果
内容的提问来源于stack exchange,提问作者Björn Svensson
相关产品推荐
相关产品推荐

