server字符集问题

  1. 获取JVM默认字符集:System.getProperty("file.encoding")
    [html]  view plain  copy
     
    1. public class Test {  
    2.     public static void main(String[] args) {  
    3.         System.out.println(System.getProperty("file.encoding"));  
    4.     }  
    5. }  
  2. 获取中文字符的长度不同
    [java]  view plain  copy
     
    1. public class Test {  
    2.     public static void main(String[] args) {  
    3.         System.out.println("测".getBytes(Charset.forName("UTF-8")).length);//结果:3  
    4.         System.out.println("测".getBytes(Charset.forName("GBK")).length);//结果:2  
    5.     }  
    6. }  
  3. 经过以上的测试得出普遍一个结果:Xp系统安装Tomcat获取默认字符集为GBK,Linux根据语言不同而不同一般从Windows下面移植过来的系统容易出现乱码(测试方法可以新建一个charset.jsp,加入如下内容)。
    [java]  view plain  copy
     
    1. <%=System.getProperty("file.encoding")%>  
  4. 统一JVM字符集的方法有两种:一种是在编码的过程中在需要指定字符集的时候,使用统一的字符集,如getBytes("GBK")等等;第二种,在Tomcat启动过程中指定JAVA_OPTS参数"-Dfile.encoding=UTF-8"
    [plain]  view plain  copy
     
    1. JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF-8"  

猜你喜欢

转载自gutou9.iteye.com/blog/2344120