Gzip is used to compress the json string on the server side, and the page ajax is decompressed after the data is requested, and there are garbled codes in Chinese.

related codes

/ / pako.js decompression code

 function unzip(b64Data){
    var strData     = atob(b64Data);
    var charData    = strData.split("").map(function(x){return x.charCodeAt(0);});
    var binData     = new Uint8Array(charData);
    var data        = pako.inflate(binData);
    strData     = String.fromCharCode.apply(null, new Uint16Array(data));
    return strData;
}

/ / java server uses gzip for compression

 
public static String gzip(String primStr) {
    if (primStr == null || primStr.length() == 0) {
        return primStr;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip = null;
    try {
        gzip = new GZIPOutputStream(out);
        gzip.write(primStr.getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (gzip != null) {
            try {
                gzip.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return new sun.misc.BASE64Encoder().encode(out.toByteArray());
}    

what result do you expect? What is the error message actually seen?

problem description

Chinese garbled after parsing into json string

the environmental background of the problems and what methods you have tried

the use of gzip is mainly to speed up the page request json data speed, in the server side of the Chinese encoding, js decoding is not good, ask for advice!

May.14,2022

getBytes () uses the default character set of the system, which may be different from the front-end character set. In the same way, you should develop a character set
when decoding in js. In this final scenario, you can directly open the compression of servlet container / nginx. It is too troublesome to write and compress yourself, and the efficiency may not be as good as these two.


have you solved it? How did you solve it?

Menu