Default Charset on Heroku (US-ASCII) causing probl

2019-04-12 03:15发布

问题:

We have a maven--jetty-based Java app deployed on heroku.

Locally, when I do:

System.out.println("Default Charset = "+ Charset.defaultCharset());
String s = "Resumé of Schrödinger";
System.out.println("s = "+ s);`

I see (as expected):

Default Charset = UTF-8
s = Resumé of Schrödinger

But, when I push the app to heroku, and check the logs, I see:

Default Charset = US-ASCII
s = Resum?? of Schr??dinger 

Actually, I'm facing further problems because of this, since we have to Decode Base-64 encoded text which has UTF-8 encoded characters.

I have even tried the following with no use:

SAXBuilder builder = new SAXBuilder();
InputStream iStream = new ByteArrayInputStream(xmlAsString.getBytes("UTF-8"));
Reader reader = new InputStreamReader(iStream, "UTF-8");
InputSource is = new InputSource(reader);
is.setEncoding("UTF-8");

And later, when doing org.apache.commons.codec.binary.Base64.decodeBase64(byte []) I am even doing stringObject.getBytes("UTF-8")

But still, I am unable to see the characters like e-acute(é), umlaut(ö), etc.

Is there any way to solve this on Heroku?

The jdk version in pom.xml is 1.6

Is it a quirk of the OpenJDK 1.7 & the virtual machine underneath in Heroku?

Thanks in advance.

回答1:

Finally, I got in touch with the friendly staff at Heroku--they gave the following suggestion to over-ride file.encoding property via the JAVA_OPTS env-variable.

Issued the following from my Heroku Toolbelt, & things began working now.

heroku config:add JAVA_OPTS='-Xmx384m -Xss512k -XX:+UseCompressedOops -Dfile.encoding=UTF-8'

This way, the JVM picks it up, & now Charset.defaultCharset( ) returns UTF-8, with special characters appearing as they should!

They also said, we could alternatively do the following as well:

heroku config:add JAVA_TOOL_OPTIONS='-Dfile.encoding=UTF-8'

Also, it would be a good idea to embed this property right into the Procfile of the app, so that our code behaves the same when we push it to a new Heroku app.



回答2:

Set file.encoding system variable. Open jdk uses this to get the default characterset.