我实现了 org.osgi.service.cm.ManagedService 接口(interface)来获取 Karaf 配置。但是当我给属性赋予中文值时,就出现乱码了。最初etc文件夹中的文件都是latin1编码的。我尝试过设置utf-8编码,但是没有效果。谁能帮我吗?
最佳答案
在 Karaf 中,配置文件(即 etc/*.cfg
)由 felix 子项目“fileinstall”处理。
fileinstall 尚不支持为配置指定自定义字符编码,它使用 Properties
类和 Properties.load(InputStream)
,其中记录:
The load(Reader) / store(Writer, String) methods load and store properties from and to a character based stream in a simple line-oriented format specified below. The load(InputStream) / store(OutputStream, String) methods work the same way as the load(Reader)/store(Writer, String) pair, except the input/output stream is encoded in ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding can be written using Unicode escapes as defined in section 3.3 of The Java™ Language Specification; only a single 'u' character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.
因此,您必须在 ISE-8859-1 中对文件进行编码并引用每个 UTF 字符,或者使用 xml 文件对配置文件进行编码。
关于character-encoding - karaf配置属性乱码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35691662/