什么GBK,UTF-8都是浮云,乱码得这样

什么GBK,UTF-8都是浮云,乱码得这样,第1张

相信很多人都会遇到乱码问题。想我当初遇到乱码时也是这里转那边转,配置文件里面改,服务器配置里面改,还用强转,改来改去最后还是乱码。

烦死人了。这不。最近又乱码了。好在上天有好生之德。 我意外知道一个东西。哟呵呵~~~解决乱码不是问题。那就是使用Base64编码,接收时再转码。保证不会出现乱码的问题。为什么?这还用说,base64转码后就不是中文了,而是全英文的~~~不多说了。程序员别的不会,先贴代码。

[java] view plain copy print

private static BASE64Encoder encoder = new BASE64Encoder();

private static BASE64Decoder decoder = new BASE64Decoder();

/

BASE64 编码

@param s

@return

/

public static String encodeBufferBase64(byte[] buff)

{

return buff == nullnull:encoderencodeBuffer(buff)trim();

}

/

BASE64解码

@param s

@return

/

public static byte[] decodeBufferBase64(String s)

{

try

{

return s == null null : decoderdecodeBuffer(s);

}

catch (IOException e)

{

eprintStackTrace();

}

return null;

}

/

base64编码

@param bytes

字符数组

@return

@throws IOException

/

public static String encodeBytes(byte[] bytes) throws IOException

{

return new BASE64Encoder()encode(bytes)replace("\n", "")replace("\r", "");

}

/

base64解码

@param bytes

字符数组

@return

@throws IOException

/

public static String decodeBytes(byte[] bytes) throws IOException

{

return new String(new BASE64Decoder()decodeBuffer(new String(bytes)));

}

测试代码如下:

[java] view plain copy print

public static void main(String[] args) throws Exception

{

String str1="我爱你";

String s=encodeBufferBase64(str1getBytes());

Systemoutprintln(s);

String strs=new String(decodeBufferBase64(s));

Systemoutprintln(strs);

}

控制台显示:

[plain] view plain copy print

ztKwrsTj

当前,绝大多数linux的默认编码是utf-8,GBK在这上面显示是乱码。解决方法有两个:

1) 将所有页面编码转为utf-8 (推荐)

2)在Linux下将默认编码设为gbk,命令行下运行如下命令:

export LANG=zh_CNGBK

以上就是关于什么GBK,UTF-8都是浮云,乱码得这样全部的内容,包括:什么GBK,UTF-8都是浮云,乱码得这样、linux 下面部署GBK编码的项目。乱码。。求帮助哇!!!、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zz/9859156.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-02
下一篇2023-05-02

发表评论

登录后才能评论

评论列表(0条)

    保存