简体   繁体   中英

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.

But in Linux it is returning "compos??s", so my DB query fails.

Can anyone give me solution?

Maybe because the Charset UTF8 doesn't exist. You must use UTF-8 . See the javadoc .

First of all, using unicode output with System.out.println is no good indicator since you are dealing with console encoding. Open the file with OutputStreamWriter , explicite setting encoding to UTF-8, then you can say if the request parameter in encoded correctly or not.

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.

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; that hack was maybe only needed on 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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