java UTF-8 文本(印地语)未显示在浏览器窗口或 Eclipse 控制台上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14772275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:21:09  来源:igfitidea点击:

UTF-8 text (Hindi) not getting displayed on Browser window or Eclipse console

javaunicodecharacter-encoding

提问by DarkKnightFan

I have to display text in Hindi (or any regional language) on the browser screens. I will be getting this text from the database.

我必须在浏览器屏幕上以印地语(或任何区域语言)显示文本。我将从数据库中获取此文本。

For this I started at a very basic level with the following:

为此,我从一个非常基本的层面开始,如下:

String escapedStr = "\u0905\u092d\u0940 \u0938\u092e\u092f \u0939\u0948 \u091c\u0928\u0924\u093e";
String hindiText = StringEscapeUtils.unescapeJava(escapedStr);
System.out.println(hindiText);
return hindiText;

I am able to get the Hindi text perfectly fine in the variable hindiText. But when I print it on eclipse console or on the browser screen I get only ???? ?? ??

我能够在变量中完美地获得印地语文本hindiText。但是当我在 eclipse 控制台或浏览器屏幕上打印它时,我只得到???? ?? ??

I set the default character encoding of my browser as well as my eclipse console to UNICODE(UTF-8). But still no success.

我将浏览器和 eclipse 控制台的默认字符编码设置为 UNICODE(UTF-8)。但仍然没有成功。

Can anyone help me solve this? What setting am I missing?

谁能帮我解决这个问题?我缺少什么设置?

Just fyi - I am able to open hindi websites in my browser. So language settings is not an issue.

仅供参考 - 我可以在浏览器中打开印地语网站。所以语言设置不是问题。

EDIT

编辑

As I am using JSP files for my views, I have added the following to my web.xmlfor setting the character encoding globally. Ref: Followed this

由于我在视图中使用 JSP 文件,因此我已将以下内容添加到我web.xml的全局设置字符编码中。参考:按照这个

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>

But still no success!

但是还是没有成功!

回答by BalusC

But when I print it on eclipse console or on the browser screen I get only ???? ?? ??

但是当我在 eclipse 控制台或浏览器屏幕上打印它时,我只得到 ???? ?? ??

As to Eclipse part, you need to tell it to use UTF-8 for its stdout console. You can set that by Window > Preferences > General > Workspace > Text File Encoding.

至于 Eclipse 部分,您需要告诉它使用 UTF-8 作为其标准输出控制台。您可以通过Window > Preferences > General > Workspace > Text File Encoding 进行设置

enter image description here

在此处输入图片说明

As to the JSP part, you need to tell it to use UTF-8 to write HTTP response body. You can set that by either

至于JSP部分,你需要告诉它使用UTF-8来编写HTTP响应体。您可以通过以下任一方式设置

<%@page pageEncoding="UTF-8"%>

in every individual JSP, or applicationwide by

在每个单独的 JSP 中,或应用程序范围内

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>

in web.xml.

web.xml.

See also:

也可以看看:

回答by scofield

use your code this way

以这种方式使用您的代码

System.out.println("\u0905\u092d\u0940\u0938\u092e\u092f\u0939\u0948\u091c");

回答by Ingo

The problem is that the output streams you use probably have the "default" encoding of your platform. On windows, this is often some crappy MS 8-bit code page.

问题是您使用的输出流可能具有您平台的“默认”编码。在 Windows 上,这通常是一些蹩脚的 MS 8 位代码页。

Make it a habit to print your text through PrintWriters, and make sure they are created with the correct encoding.

养成通过 PrintWriter 打印文本的习惯,并确保它们是使用正确的编码创建的。