Java convert Windows-1252 to UTF-8, some letters are wrong

后端 未结 2 2076
小蘑菇
小蘑菇 2020-12-18 08:45

I receive data from a external Microsoft SQL 2008 database (I make queries with MyBatis). The data is encoded as "Windows-1252".

I have tried to re-encode t

2条回答
  •  情话喂你
    2020-12-18 09:37

    I solved it thanks to all.

    I have the next project structure:

    • MyBatisQueries: I have a query with a "select" which gives me the String
    • Pojo to save the String (which gave me the String with conversion problems)
    • The class which uses the query and the Pojo object with data (that showed me bad decoded)

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

    The solution:

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

提交回复
热议问题