English 中文(简体)
用Java处理法语字符
原标题:Handle French Characters in Java

我有一个页面,我在那里搜索一个术语,它显示得很完美。不管是什么类型的角色。

现在,当我在JSP中有几个复选框时,我会选中它并提交。在这些复选框中,我有一个框名,类似于ABC Farmacéutica Corporation

当我点击提交按钮时,我正在调用一个函数,并将所有参数设置为一个表单,然后提交该表单。

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

Please help... Thanks in advance.

编辑:

请尝试这个示例

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);      
        }
    }
}

我得到的输出是,

aaa   PiSA+Farmac%C3%A9utica+Mexicana+Corporativo
bbb   PiSA Farmacéutica Mexicana Corporativo

尝试按原样打印字符串aaa

最佳回答

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

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

问题回答

您会得到“ABC Farmacâ©utica Corporation”,因为您从客户端收到的字符串是ISO-8859-1。在对其进行URL解码之前,您需要将其转换为UTF-8

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

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

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.

据我所知,文本被硬编码在控制器代码中,如下所示:

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

我预计这会起作用:

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

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

例如,如果编辑器将Java文件保存为UTF-8,并且在UTF-8不是默认编码,则需要告诉编译器使用该编码:

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

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

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

在File>;属性然后在jsp页面的头部添加以下行:<code><;%@page language=“java”contentType=“text.html;charset=UTF-8”pageEncoding=“UTF-8”%>





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签