javascript 在 Java 中处理法语字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6213377/
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-25 19:53:38  来源:igfitidea点击:

Handle French Characters in Java

javajavascriptencoding

提问by Max

I Have a Page where I search for a term and it is displaying perfect. Whatever character type it is.

我有一个页面,我可以在其中搜索一个术语,并且显示完美。不管它是什么字符类型。

Now when I have few checkboxes in JSP and I check it and submit. In these checkboxes I have one box name like ABC Farmacéutica Corporation.

现在,当我在 JSP 中有几个复选框时,我检查并提交。在这些复选框中,我有一个框名称,例如ABC Farmacéutica Corporation.

When I click on submit button, I am calling a function and will set all parameters to a form and will submit that form. (I tested putting alert for the special character display before submit and it is displaying good).

当我单击提交按钮时,我正在调用一个函数并将所有参数设置为一个表单并提交该表单。(我在提交之前测试了特殊字符显示的警报,它显示良好)。

Now, coming to the Java end, I use Springs Frame work. When I print the term in controller, then it is displayed like ABC Farmac??utica Corporation.

现在,到了 Java 端,我使用 Springs Frame 工作。当我在控制器中打印术语时,它显示为ABC Farmac??utica Corporation.

Please help... Thanks in advance.

请帮助...提前致谢。

EDIT :

编辑 :

Please try this sample Example

请尝试此示例示例

import java.net.*;
class sample{
    public static void main(String[] args){
        try{
            String aaa = "ABC Farmacéutica Corporation";
            String bbb = "ABC Farmac??utica Corporation";

            aaa = URLEncoder.encode(aaa, "UTF-8");
            bbb = URLDecoder.decode(bbb, "UTF-8");

            System.out.println("aaa   "+aaa);
            System.out.println("bbb   "+bbb);

        }catch(Exception e){
            System.out.println(e);      
        }
    }
}

I am getting output as,

我得到的输出是,

aaa   PiSA+Farmac%C3%A9utica+Mexicana+Corporativo
bbb   PiSA Farmac├?utica Mexicana Corporativo

Try to print the string aaaas it is.

尝试按string aaa原样打印。

采纳答案by Thorbj?rn Ravn Andersen

This is an encoding problem, and the ?clearly identify that this is UTF-8 text interpreted as ISO-Latin-1 (or one of its cousins).

这是一个编码问题,并且?清楚地识别出这是解释为 ISO-Latin-1(或其表亲之一)的 UTF-8 文本。

Ensure that your JSP-page at the top show that it uses UTF-8 encoding.

确保顶部的 JSP 页面显示它使用 UTF-8 编码。

回答by Yanick Rochon

You get "ABC Farmac??utica Corporation"because the string you receive from the client is ISO-8859-1, you need to convert it into UTF-8before you URL decode it. Like this :

你得到"ABC Farmac??utica Corporation"是因为你从客户端收到的字符串是ISO-8859-1,你需要UTF-8在 URL 解码之前将它转换成它。像这样 :

bbb = URLDecoder.decode(new String(bbb.getBytes("ISO-8859-1"), "UTF-8"), "UTF-8");

NOTE: some encodings cannot be converted from and to different encodings without risking data loss. For example, you cannot convert Tha? characters (TIS-620) to another encoding, not even UTF-8. For this reason, avoid converting from one encoding to another, unless ultimately necessary (ie. the data comes from an external, third perty, or proprietary source, etc.) This is only a solution on how to convert from one source to another, knowingthe source encoding.

注意:有些编码不能在不存在数据丢失风险的情况下转换为不同的编码。例如,您不能转换 Tha?字符 ( TIS-620) 到另一种编码,甚至不是UTF-8. 出于这个原因,避免从一种编码转换为另一种编码,除非最终有必要(即数据来自外部、第三方或专有来源等)。这只是关于如何从一种来源转换为另一种来源的解决方案,知道源编码。

回答by McDowell

As I understand it, the text is hardcoded in controller code like this:

据我了解,文本是在控制器代码中硬编码的,如下所示:

    ModelAndView mav = new ModelAndView("hello");
    mav.addObject("message", "ABC Farmacéutica Corporation");
    return mav;

I expect this would work:

我希望这会奏效:

    ModelAndView mav = new ModelAndView("hello");
    mav.addObject("message", "ABC Farmac\u00e9utica Corporation");
    return mav;

If so, the problem is due to a mismatch between the character encoding your Java editor is using and the encoding your compiler uses to read the source code.

如果是这样,则问题是由于 Java 编辑器使用的字符编码与编译器用于读取源代码的编码不匹配。

For example, if your editor saves the Java file as UTF-8 and you compile on a system where UTF-8 is not the default encoding, then you would need to tell your compiler to use that encoding:

例如,如果您的编辑器将 Java 文件保存为 UTF-8 并且您在 UTF-8 不是默认编码的系统上进行编译,那么您需要告诉您的编译器使用该编码:

javac -cp foo.jar -encoding UTF-8 Bar.java

Your build scripts and IDE settings need to be consistent when handling character data.

在处理字符数据时,您的构建脚本和 IDE 设置需要保持一致。

If your text editor saved your file as UTF-8 then, in a hex editor, é would be the byte sequence C3 A9; in many other encodings, it would have the value E9. ISO-8859-1 and windows-1252 would encode ?? as C3 A9. You can read about character encoding in Java source files here.

如果您的文本编辑器将您的文件保存为 UTF-8,那么在十六进制编辑器中,é 将是字节序列C3 A9;在许多其他编码中,它的值为E9。ISO-8859-1 和 windows-1252 会编码 ?? 作为C3 A9。您可以在此处阅读 Java 源文件中的字符编码。

回答by Liv

I suspect the problem is with character encoding on the page. Make sure the page you submit from and the one you display to use the same character set and make sure that you set it explicitely. for instance if your server runs on Linux the default encoding will be UTF-8 but if you view the page on Windows it will assume (if no encoding is specified) it to be ISO-8859-1. Also when you are receiving the submitted text on your server side, the server will assume the default character set when building the string -- whereas your user might have used a differrent encoding if you didn't specify one.

我怀疑问题出在页面上的字符编码上。确保您提交的页面和显示的页面使用相同的字符集,并确保明确设置它。例如,如果您的服务器在 Linux 上运行,默认编码将是 UTF-8,但如果您在 Windows 上查看页面,它将假定(如果未指定编码)它是 ISO-8859-1。此外,当您在服务器端接收提交的文本时,服务器将在构建字符串时采用默认字符集——而如果您没有指定,您的用户可能使用了不同的编码。

回答by Hafsa

Change the encoding of jsp page to UTF-8 in the File> Properties then add this line in the head of your jsp page: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

在文件>属性中将jsp页面的编码更改为UTF-8,然后在jsp页面的头部添加这一行: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>