如何以编程方式正确设置JVM(1.5.x)使用的默认字符编码?
我已经读过-Dfile.encoding=whatever以前用于旧JVM的方法......由于我不会进入的原因,我没有那么奢侈.
我试过了:
System.setProperty("file.encoding", "UTF-8");
Run Code Online (Sandbox Code Playgroud)
并且属性已设置,但它似乎不会导致下面的最终getBytes调用使用UTF8:
System.setProperty("file.encoding", "UTF-8");
byte inbytes[] = new byte[1024];
FileInputStream fis = new FileInputStream("response.txt");
fis.read(inbytes);
FileOutputStream fos = new FileOutputStream("response-2.txt");
String in = new String(inbytes, "UTF8");
fos.write(in.getBytes());
Run Code Online (Sandbox Code Playgroud) 我需要在使用Java的资源属性中使用UTF-8 ResourceBundle.当我将文本直接输入属性文件时,它显示为mojibake.
我的应用在Google App Engine上运行.
谁能举个例子?我无法完成这项工作.
java google-app-engine resourcebundle utf-8 internationalization
我试图找到一种方法来为@ValueSpring Boot中的application.property文件中的注释访问的属性设置UTF-8编码.到目前为止,我已经通过创建bean成功地将编码设置为我自己的属性源:
@Bean
@Primary
public PropertySourcesPlaceholderConfigurer placeholderConfigurer(){
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setLocation(new ClassPathResource("app.properties");
configurer.setFileEncoding("UTF-8");
return configurer;
}
Run Code Online (Sandbox Code Playgroud)
这种解决方案存在两个问题 这一次,它不适用于Spring Boot默认使用的"application.properties"位置(http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config .html #boot-features-external-config),我被迫使用不同的文件名.
另一个问题是,通过它我可以手动定义和排序多个源的支持位置(例如,在jar与外部jar属性文件等中),从而重做已经完成的工作.
如何获取对已配置的PropertySourcesPlaceholderConfigurer的引用,并在应用程序初始化的恰当时间更改其文件编码?
编辑:也许我在其他地方犯了错误?这就是导致实际问题的原因:当我使用application.properties允许用户将个人名称应用于从应用程序发送的电子邮件时:
@Value("${mail.mailerAddress}")
private String mailerAddress;
@Value("${mail.mailerName}")
private String mailerName; // Actual property is ?wi?ty Miko?aj
private InternetAddress getSender(){
InternetAddress sender = new InternetAddress();
sender.setAddress(mailerAddress);
try {
sender.setPersonal(mailerName, "UTF-8"); // Result is Święty Mikołaj
// OR: sender.setPersonal(mailerName); // Result is ??wiÄ?ty Miko??aj
} catch (UnsupportedEncodingException e) {
logger.error("Unsupported encoding used in sender …Run Code Online (Sandbox Code Playgroud) 尽管 Java 属性文件传统上仅支持 ISO-8859-1,但JDK 9 及更高版本支持以 UTF-8 编码的属性文件。虽然只有 JDK 9+ 通过内置默认属性文件读取支持 UTF-8,但它使用的相同技术可以在任何 Java 版本中完成,围绕属性文件添加 UTF-8 支持并回退到 ISO-8859 -1 用于向后兼容(正如 JDK 实现所做的那样)。
我是 Spring Boot 的新手,我正在阅读它带来的所有漂亮的属性配置。Spring Boot 是否支持从以 UTF-8 编码的属性文件加载属性?如果没有,Spring Boot 代码中的何处是属性文件读取合并,以便我可以添加此功能并提交拉取请求?