简体   繁体   English

linux中的UTF-8问题

[英]UTF-8 issue in linux

String departmentName = request.getParameter("dept_name");
departmentName = new String(departmentName.getBytes(Charset.forName("UTF8")),"UTF8");
System.out.println(departmentName);//O/p:  composés

In windows, the displayed output is what I expected and it is also fetching the record on department name matching criteria. 在Windows中,显示的输出是我所期望的,它也是在部门名称匹配条件上获取记录。

But in Linux it is returning "compos??s", so my DB query fails. 但是在Linux中它返回“compos ?? s”,所以我的数据库查询失败了。

Can anyone give me solution? 谁能给我解决方案?

Maybe because the Charset UTF8 doesn't exist. 也许是因为Charset UTF8不存在。 You must use UTF-8 . 您必须使用UTF-8 See the javadoc . 请参阅javadoc

First of all, using unicode output with System.out.println is no good indicator since you are dealing with console encoding. 首先,使用带有System.out.println unicode输出并不是一个好的指标,因为您正在处理控制台编码。 Open the file with OutputStreamWriter , explicite setting encoding to UTF-8, then you can say if the request parameter in encoded correctly or not. OutputStreamWriter打开文件,将explicite设置编码打开到UTF-8,然后你可以说是否正确编码了请求参数。

Second, there may be database connection encoding issue. 其次,可能存在数据库连接编码问题。 For MySQL you need to explicite specify encoding in connection string, as for other, it could also be, that the default system encoding is taken, when not specified. 对于MySQL,您需要在连接字符串中明确指定编码,对于其他情况,也可以是未指定时采用默认系统编码。

First of all, try to figure out the encoding you have in every particular place. 首先,尝试找出每个特定位置的编码。

For example, the string might already have the proper encoding, if your Linux system is running with UTF-8 charset; 例如,如果Linux系统使用UTF-8字符集运行,则字符串可能已经具有正确的编码; that hack was maybe only needed on Windows. 那个黑客可能只在Windows上需要。

Last but not least, how do you know it is incorrect? 最后但并非最不重要的,你怎么知道这是不正确的? And it is not your viewer that is incorrect? 你的观众不是不正确吗? What character set does your console or log viewer use? 您的控制台日志查看器使用什么字符集?

You need to perform a number of checks to find out where exactly the encoding is different from what is expected at that point of your application. 您需要执行大量检查以确定编码与应用程序的那个点的预期完全不同。

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

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