简体   繁体   English

Java Linux字符编码问题

[英]Java linux character encoding issue

I'm facing an issue with character encoding in linux. 我在linux中遇到字符编码问题。 I'm retrieving a content from amazon S3, which was saved using UTF-8 encoding. 我正在从Amazon S3检索内容,该内容是使用UTF-8编码保存的。 The content is in Chinese and I'm able to see the content correctly in the browser. 内容为中文,我可以在浏览器中正确看到内容。

I'm using amazon SDK to retrieve the content and do some update to it.Here's the code I'm using: 我正在使用Amazon SDK检索内容并对其进行一些更新,这是我正在使用的代码:


StringBuilder builder = new StringBuilder();
S3Object object = client.getObject(new GetObjectRequest(bucketName, key));
        BufferedReader reader = new BufferedReader(new 
                InputStreamReader(object.getObjectContent(), "utf-8"));
while (true) {
    String line = reader.readLine();
    if (line == null) 
        break;
    builder.append(line);
}

This piece of code works fine in Windows environment as I was able to update the content and save it back without messing up any chinese characters in it. 这段代码在Windows环境中运行良好,因为我能够更新内容并将其保存回去而不会弄乱其中的任何汉字。

But, its acting differently in linux enviroment. 但是,它在linux环境中的行为有所不同。 The code is unable to translate the characters properly, the chinese characters are rendered as ??? 该代码无法正确翻译字符,汉字呈现为???

I'm not sure what's going wrong here. 我不确定这里出了什么问题。 Any pointers will be appreciated. 任何指针将不胜感激。

-Thanks -谢谢

The default charset is different for the 2 OS's your using. 您使用的2个操作系统的默认字符集不同。

To start off, you can confirm the difference by printing out the default charset. 首先,您可以通过打印默认字符集来确认差异。

Charset.defaultCharset.name()

Somewhere in your code, I think this default charset is being used for some String conversion. 在您代码的某个位置,我认为此默认字符集已用于某些String转换。 The correct procedure should be to track that down, and specify UTF-8. 正确的过程应该是进行跟踪,并指定UTF-8。

Without seeing that code, I can only suggest the 'cheating' way to do it: set the default charset explicitly, near the beginning of your code, or at Java startup. 在看不到该代码的情况下,我只能提出一种“作弊”的方式:在代码的开头附近或在Java启动时显式设置默认字符集。 See here for changing default charset: Setting the default Java character encoding? 请参阅此处以更改默认字符集: 设置默认Java字符编码?

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM