[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]Java虚拟机默认编码Java virtual machine default charset

上一篇:[转帖]kibana,用nginx跳转做Basic认证。
下一篇:[备忘]HttpClient如何Gzip压缩。

添加日期:2017/3/31 12:31:35 快速返回   返回列表 阅读2218次
涉及到字节与字符串转换的很多地方,如果不指定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")


 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved