java - 从应用程序读取属性时获取不需要的字符。属性文件
问题描述
我正在尝试从应用程序属性文件中读取值。它在本地(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)));
解决方案
在您的 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"
推荐阅读
- javascript - Animate.css 承诺有时不会在动画结束后清理类
- node.js - 使用 Mongoose 在 MongoDB 上避免重复
- flutter - 无状态小部件列固定白色
- ios - Swift:如何覆盖 Firebase 数据库上的值?
- r - 替换数据框中的单元格
- mongodb - 无法连接到 MongoDB - 身份验证失败
- django - Django 中的媒体文件
- sql-server - 从 VBA excel 中的两个 SQL 数据库中检索数据
- visual-studio-code - VSCode Remote SSH 无法识别私钥文件
- python - JSON 构建对象使用时区删除时间戳中的尾随 0