java string乱码
题目出在预发、生产和当地环境的体系编码体式格局不一致,当地体系默许是UTF-8,而预发、生产环境默许是GBK编码,因而致使涌现乱码。
假如不指定编码体式格局,则默许以体系的编码体式格局。
String csn = Charset.defaultCharset().name(); try { // use charset name decode() variant which provides caching. return decode(csn, ba, off, len); } catch (UnsupportedEncodingException x) { warnUnsupportedCharset(csn); } try { return decode("ISO-8859-1", ba, off, len); } catch (UnsupportedEncodingException x) { // If this code is hit during VM initialization, MessageUtils is // the only way we will be able to get any kind of error message. MessageUtils.err("ISO-8859-1 charset not available: " + x.toString()); // If we can not find ISO-8859-1 (a required encoding) then things // are seriously wrong with the installation. System.exit(1); return null; } System.getProperty("file.encoding") //检察体系默许编码体式格局
解决方法以下:
1、运用string时举行转码
System.out.println(str); String str1 = new String(str.getBytes("ISO-8859-1"), "utf-8"); System.out.println(str1); String str2 = new String(str.getBytes("gb2312"), "utf-8"); System.out.println(str2); String str3 = new String(str.getBytes("gbk"), "utf-8"); System.out.println(str3);
2、将乱码的字符串举行转码
String decodeStr=null; decodeStr = URLDecoder.decode(url, "utf-8");
因而在运用String的时刻,不管 encode 或许 decode都要指定编码体式格局,不然就和体系环境耦合了。
ki4网,大批的免费Java入门教程,迎接在线进修!
以上就是java string乱码的细致内容,更多请关注ki4网别的相干文章!