Java将Windows-1252转换为UTF-8,有些字母错误 [英] Java convert Windows-1252 to UTF-8, some letters are wrong
问题描述
我从外部Microsoft SQL 2008数据库接收数据(我通过MyBatis进行查询).在理论中,我在"Windows-1252"上接收了数据编码.
I receive data from external Microsoft SQL 2008 Data base (I make Queries with MyBatis). In theroy I receive data encoding on "Windows-1252".
我尝试使用以下代码解码数据:
I try decoded data with this code:
字符串textoFormado = ...来自MyBatis的值...;
String textoFormado = ...value from MyBatis... ;
String s = new String(textoFormado.getBytes("Windows-1252"),"UTF-8");
String s = new String(textoFormado.getBytes("Windows-1252"), "UTF-8");
几乎所有的String都已正确解码.但是有些字母不带音符.
Almost all the String is correctly decoded. But some letter with acents not.
例如:
- 我从数据库收到以下字符串:Ã?vila"
- 我使用上面的代码,这使它成为String:.?vila"
- 我希望这个字符串:Ávila"
推荐答案
感谢所有人.
我有下一个项目结构:
- MyBatisQueries:我有一个带有选择"的查询,该查询为我提供了字符串
- Pojo保存字符串(这给了我带有转换问题的字符串)
- 使用查询和数据的Pojo对象的类(表明解码错误)
起初我有(MyBatis和Spring注入依赖项和参数):
at first I had (MyBatis and Spring inject dependencies and params):
public class Pojo {
private String params;
public void setParams(String params) {
try {
this.params = params;
}
}
}
解决方案:
public class Pojo {
private String params;
public void setParams(byte[] params) {
try {
this.params = new String(params, "UTF-8");
} catch (UnsupportedEncodingException e) {
this.params = null;
}
}
}
这篇关于Java将Windows-1252转换为UTF-8,有些字母错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!