package com.yin.test; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; /** * @author User * */ public class MyTestClass { public static void main(String[] args) { String str = "测试字符转换 a beautiful girl"; //默认环境,已是UTF-8编码 try { String strGBK = URLEncoder.encode(str, "GBK"); System.out.println(strGBK); String strUTF8 = URLDecoder.decode(str, "UTF-8"); System.out.println(strUTF8); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }
转码技术一般用于数据传递,防止中文乱的出现,导致数据异常~