首页 > 解决方案 > 从应用程序读取属性时获取不需要的字符。属性文件

问题描述

我正在尝试从应用程序属性文件中读取值。它在本地(Windows 机器)上运行良好,但是当我在 aws 上部署我的代码时,我在变量的值上附加了一些不需要的字符,例如 /n 在值的末尾。我正在使用 Springboot

我们尝试在启动时将所有属性放入地图并记录它们。我们没有发现任何 %0a 字符附加到值中。这是代码片段 -

 Properties prop = new Properties();
    try(final InputStream stream =       
 Application.class.getClassLoader().getResourceAsStream("application- 
    dev.properties")) {
        prop.load(stream);
        System.out.println(prop.values().toString());

        Set set = prop.entrySet();
        Iterator itr = set.iterator();
        while (itr.hasNext()) {
            Map.Entry entry = (Map.Entry)itr.next();
            log.info("key as Normal String "+entry.getKey());
            log.info("key as URl Encoded "+ URLEncoder.encode(String.valueOf(entry.getKey()), String.valueOf(StandardCharsets.UTF_8)));

            log.info("Value as Normal String "+entry.getValue());
            log.info("Value as URl Encoded "+ URLEncoder.encode(String.valueOf(entry.getValue()), String.valueOf(StandardCharsets.UTF_8)));

标签: javaspringamazon-web-servicesspring-bootcharacter-encoding

解决方案


在您的 EC2 实例中,您需要添加file.encoding参数并设置为UTF-8.

我们可以在以下位置使用 maven 传递此属性jvmArguments

mvn spring-boot:run -Drun.jvmArguments="-Dfile.encoding=UTF-8"

此外,您可以导出为JAVA_TOOL_OPTIONS

export JAVA_TOOL_OPTIONS="-Dfile.encoding=UTF-8"

推荐阅读