涉及到字节与字符串转换的很多地方,如果不指定Charset,就会使用默认的编码
即: java.nio.charset.Charset.defaultCharset()
/** * Returns the default charset of this Java virtual machine. * * <p> The default charset is determined during virtual-machine startup and * typically depends upon the locale and charset of the underlying * operating system. * * @return A charset object for the default charset * * @since 1.5 */ public static Charset defaultCharset() { if (defaultCharset == null) { synchronized (Charset.class) { String csn = AccessController.doPrivileged( new GetPropertyAction("file.encoding")); Charset cs = lookup(csn); if (cs != null) defaultCharset = cs; else defaultCharset = forName("UTF-8"); } } return defaultCharset; }
可以看到,会找file.encoding属性,没有的话,会使用UTF-8. 这倒是很方便啊 想指定默认编码的话,就这样: java -Dfile.encoding=UTF8 java虚拟机启动了,这个值就固定了。 ----------------------------------------------------- 如new InputStreamReader(request.getInputStream()) 就会使用默认编码
还是显式指定比较好 new InputStreamReader(request.getInputStream(),"UTF-8")
|