开发者

Google App Engine encoding

I'm trying to log some russian text:

LOG.info("тестирование русского");

But I get question symbols instead (viewing from web):

[app-id/app-version].: 15:18:44,753 INFO [class] - ???????????? ????????

Java file saved with UTF-8 encoding. All settings are default.

Even I read file in UTF-8 with russian characters and try to log something from it -- encoding is wro开发者_开发知识库ng too.


I had a similar problem with Hebrew text. I found out it was caused by the default encoding.

To check the default encoding, I used this code:

OutputStreamWriter out = new OutputStreamWriter(new ByteArrayOutputStream());
String encoding = out.getEncoding(); 

On my computer, the encoding is "UTF8". On the GAE server, it is "ASCII".

I solved the problem by replacing all the file readers in my code with:

new InputStreamReader(new FileInputStream(file), "UTF8"));

This tells Java to ignore the default encoding, and open all input files as UTF8.


Try this. Apparently GAE tries to autodetect encoding and fails. Even constant strings were messed up

public class Util {
    public static String FixRussianString(String string){
        try {
            return new String(string.getBytes("CP1251"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return string;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜