Home  >  Article  >  Java  >  How to deal with garbled code in Java background

How to deal with garbled code in Java background

藏色散人
藏色散人Original
2020-04-11 10:00:142178browse

How to deal with garbled code in Java background

What should I do if the java background is garbled?

Solution to Chinese garbled code in java background

//将乱码字符串按照错误的编码方式转换为原始的字符串序列
byte[] bs = 乱码字段.getBytes("ISO8859-1");
//将原始的字符串序列按照正确的编码转换为正确的文字即可
新字段 = new String(bs,"UTF-8");

The second is that spring provides an encoding filter and just add the following code to web.xml

Recommended tutorial: "java learning"

<!--编码过滤器 -->
    <filter>
        <description>字符集过滤器</description>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <description>字符集编码</description>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <!-- 路径映射 -->
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

The above is the detailed content of How to deal with garbled code in Java background. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn