简体   繁体   English

"编码名称是 UTF8 还是 UTF-8?"

[英]Is the encoding name UTF8 or UTF-8?

Some httpclient requires me to give a string charsetµ.一些 httpclient 要求我提供一个字符串 charsetµ。

I am wondering whether to give it UTF8 or utf8 or UTF-8?我想知道是否给它 UTF8 或 utf8 或 UTF-8?

Especially when calling Charset.forName(enc)特别是在调用 Charset.forName(enc) 时

"

UTF-8 is the standard one and most people use it. UTF-8是标准的,大多数人都使用它。

Here is the list of charsets used in Java: 以下是Java中使用的字符集列表:

http://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html http://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html

NB NB
If charset factory/singleton classes can take Enum as parameter, as well as String , Enum is, IMO, always a safer and better option. 如果charset factory / singleton类可以将Enum作为参数,以及StringEnum是IMO,总是更安全和更好的选择。

更容易使用StandardCharsets.UTF_8,因为它可以节省您必须处理UnsupportedEncodingExceptions

FYI, For Java 7/tomcat 8.5 I am using: -Dfile.encoding=UTF-8 -Dclient.encoding.override=UTF-8 仅供参考,对于Java 7 / tomcat 8.5我正在使用:-Dfile.encoding = UTF-8 -Dclient.encoding.override = UTF-8

But for Java 8/tomcat 8.5 I got: java.nio.charset.IllegalCharsetNameException: UTF-8 但是对于Java 8 / tomcat 8.5我得到了:java.nio.charset.IllegalCharsetNameException:UTF-8

So I changed to UTF8 所以我换成了UTF8

The standard name is 'UTF-8'. 标准名称为“UTF-8”。 Source code(I use jdk1.8.0_20) showes more detail: 源代码(我使用jdk1.8.0_20)显示更多细节:

UTF_8 extends Unicode {
    public UTF_8() {
        super("UTF-8", StandardCharsets.aliases_UTF_8); // show other aliases
        // static final String[] aliases_UTF_8 = new String[]{"UTF8", "unicode-1-1-utf-8"};
    }

    public String historicalName() {
        // return old name of UTF-8
        return "UTF8";
    }
Charset charset = Charset.forName("cp1254"); 

This was the utf-8 solution for me.这对我来说是 utf-8 解决方案。 You can use it.你可以使用它。

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

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