骆驼:改变流编码

Arc*_*her 2 apache-camel

我正在使用该路由从http接收数据流:

from("direct:foo").
to("http://foo.com/bar.html").
to("file:///tmp/bar.html")
Run Code Online (Sandbox Code Playgroud)

HTTP流带有Windows-1251编码.我想在运行中重新编码UTF-8然后存储到文件.

如何使用标准camel方式?

Ral*_*alf 7

我认为vikingsteve的解决方案错过了一步.输入流包含编码为CP1251的字符.将输入流内容转换为字符串时,该流中的字符不会更改其编码.您需要指定在解码时对编码字符的实体使用的相同字符编码方案.否则你会得到不良结果.

<route id="process_umlaug_file" startupOrder="2">
    <from uri="file:///home/steppra1/Downloads?fileName=input_umlauts.txt"/>
    <convertBodyTo type="java.lang.String" charset="ISO-8859-1"/>
    <to uri="file:///home/steppra1/Downloads?fileName=output_umlauts.txt&amp;charset=UTF-8"/>
</route>
Run Code Online (Sandbox Code Playgroud)

我测试了这个读取包含德语变音符号的CP1251编码文件:

steppra1@steppra1-linux-mint ~/Downloads $ file input_umlauts.txt 
input_umlauts.txt: ISO-8859 text, with CRLF line terminators

steppra1@steppra1-linux-mint ~/Downloads $ file output_umlauts.txt 
output_umlauts.txt: UTF-8 Unicode text, with CRLF line terminators
Run Code Online (Sandbox Code Playgroud)

使用解码然后重新编码的两个步骤产生正确编码的德语变音符号.如果我改变以上路线

<route id="process_umlaug_file" startupOrder="2">
    <from uri="file:///home/steppra1/Downloads?fileName=input_umlauts.txt"/>
    <convertBodyTo type="java.lang.String" charset="UTF-8"/>
    <to uri="file:///home/steppra1/Downloads?fileName=output_umlauts.txt"/>
</route>
Run Code Online (Sandbox Code Playgroud)

然后输出文件仍然是UTF-8编码,可能是因为这是我的平台默认值,但是变音符号是乱码.


vik*_*eve 6

请看一下.convertBodyTo()- 尤其是charset论证.

from("direct:foo").
to("http://foo.com/bar.html").
convertBodyTo(String.class, "UTF-8")
to("file:///tmp/bar.html")
Run Code Online (Sandbox Code Playgroud)

参考:http://camel.apache.org/convertbodyto.html