简体   繁体   English

字符编码不适用于日语,中文和韩语

[英]Character Encoding not working for Japanese ,Chinese and Korean

I have Unicode characters for all the European countries and for a few Asian countries like Japan, China, Korean. 我为所有欧洲国家和一些亚洲国家(例如日本,中国,韩国)使用Unicode字符。 All the Unicodes are working fine for European countries except for Japan, China, Korean. 除日本,中国,韩语外,所有Unicode都适用于欧洲国家/地区。

Example for Japan: 日本的示例:

dear_name=\u30c7\u30a3\u30fc\u30e9\u30fc

Example for China: 中国示例:

dear_name=\u4eb2\u7231\u7684

Example for Korean: 韩文示例:

dear_name=\uce5c\uc560\ud558\ub294

Example for Sweden (this one is working fine): 瑞典的例子(这个很好用):

dear_name=Till

Default character encoding is UTF-8. 默认字符编码为UTF-8。

Template template = VelocityFactory.getTemplate("test.vm", "UTF-8");
   String messageText = VelocityFactory.merge(context, template, charset);

While debuging the merge method I found out that the merged result is getting grabled here itself for chinese,Japanese,korean. 在调试合并方法时,我发现对于中文,日文,韩文来说,合并结果本身就在这里被抓住。

public static String merge(VelocityContext context, Template template, String charset) throws Exception {

        String newResult = null;

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        OutputStreamWriter streamWriter;
        if(charset != null && charset.length() > 0) {
            streamWriter = new OutputStreamWriter(outputStream, charset);
        } else {
            streamWriter = new OutputStreamWriter(outputStream);
        }

        template.merge(context, streamWriter);
        streamWriter.close(); 

        mergedResult = outputStream.toString();                 
        outputStream.close();

        return newResult;        
    }
}

Below is the mail template and only for header it is displaying in correct format for Japanese, Chinese, and Korean, but not for the body: 以下是邮件模板,仅用于标题,它以正确的格式显示日文,中文和韩文,但不显示正文:

<html>
    <head>      
        <meta http-equiv="Content-Type" content="$contentType">
    </head>
    <body>
        <div id="content">
            <table border="0" cellpadding="0" cellspacing="0" style="margin-left: 0px;">
                <tr>
                    <td>
                        <table border="0" cellpadding="0" cellspacing="0" class="textBody" style="margin-bottom: 120px;">
                            <tr>
                                <td valign="bottom" class="mainHeader" nowrap>
                                    $velocityUtils.getMessage("test")
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2">
                                    <img src="$imageBar" class="clipped">
                                </td>
                            </tr>
                        </table>
                        <div id="info" class="textBody">$velocityUtils.getMessage("test1")<br><br></div>    
                    </td>
                </tr>
            </table>
        </div>
    </body>
</html>

Any information how to fix this? 任何信息如何解决此问题? How do i encode correctly?? 我如何正确编码?

尝试将其添加到您的JSP的顶部

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

you need to specify the character sets for japanese, korean and chinese 您需要指定日文,韩文和中文的字符集

For japanese try: charset=iso-2022-jp
For korean try: charset=iso-2022-kr
For chinese try: charset=big5

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

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