• java 解决中文乱码


     1 //1.代码解决
     2 public class LuanMa {
     3 
     4     public static String getNewString(String luanma){
     5         String result = "";
     6         try{
     7             result = new String(luanma.getBytes("ISO-8859-1"),"UTF-8");
     8               //以ISO-8859-1解码,再以UTF-8重新编码
     9            } catch(Exception e){
    10               e.printStackTrace(); 
    11            } 
    12          return result; 
    13     }
    14 }
    15 
    16 2.如果是web项目
    17 
    18 web.xml下添加
    19 
    20     <!-- 乱码 -->
    21 
    22     <filter>
    23 
    24       <filter-name>CharacterEncodingFilter</filter-name>
    25 
    26       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    27 
    28       <init-param>
    29 
    30         <param-name>encoding</param-name>
    31 
    32         <param-value>UTF-8</param-value>
    33 
    34       </init-param>
    35 
    36       <init-param>
    37 
    38         <param-name>forceEncoding</param-name>
    39 
    40         <param-value>true</param-value>
    41 
    42       </init-param>
    43 
    44   </filter>
    45 
    46   <filter-mapping>
    47 
    48     <filter-name>CharacterEncodingFilter</filter-name>
    49 
    50     <url-pattern>/*</url-pattern>
    51 
    52   </filter-mapping>
    53 
    54   <!-- 乱码 -->
    55 
    56 3.jsp页面乱码问题
    57 
    58 ①、一般情况
    59 
    60 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
    61 
    62 浏览器的编码也设置为UTF-8
    63 
    64  
    65 
    66 ②、对于post请求传来的数据,如果乱码,可以在乱码的数据前设置:
    67 
    68 <%request.setCharacterEncoding("UTF-8") %>
    69 
    70  
    71 
    72 ③、对于get请求,可以修改tomcat服务器的编码,解决所有的问题
    73 
    74 修改tomcat的service.xml文件,源tomcat和eclipse的tomcat都要修改
    75 
    76   参考tomcat文档http://127.0.0.1:8080/docs/config/http.html
    77 
    78   <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" useBodyEncodingForURI="true"/>
    79 
    80 <!--useBodyEncodingForURI="true"设置中文乱码问题
    81 
    82 
    83 
    84  
  • 相关阅读:
    Cassandra
    【CISCO强烈推荐】生成树 《路由协议》 卷一二 拥塞:网络延迟 阻塞:进程中 MTU QS:服务质量 OSPF RIP ISIS BGP 生成树 《路由协议》 卷一二
    m*n matrix min rank square matrix
    Moving Computation is Cheaper than Moving Data
    SASL mechanism
    一阶 斜率 二阶 原函数的粗糙度 roughness
    Linux 虚拟内存盘
    Bloom Filters
    R Tree
    释放内存
  • 原文地址:https://www.cnblogs.com/wwzyy/p/4663634.html
Copyright © 2020-2023  润新知