栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java转换字符串编码格式的方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java转换字符串编码格式的方法

java转换字符串编码格式 (解码错误,重新解码)

字符集概念:规定了某个文字对应的二进制数字存放方式(编码)和某串二进制数值代表了哪个文字(解码)的转换关系。

我们在计算机屏幕上看到的是实体化的文字,而在计算机存储介质中存放的实际是二进制的比特流。 

乱码场景(纯属瞎掰):

1) 前台输入utf-8编码的一串汉字(string1)。 (页面编码为utf-8, 在内存中会将这串汉字以utf-8编码为对应的二进制流存储)

2) 这串汉字(string1)的二进制流在经过http协议传输到后台时,这段比特流会被以iso-8859-1编码强行解码为字符串(string2)。

(2.1 http默认编码格式为iso-8859-1)

(2.2 这个默认编码在什么时候起作用呢? 应该是在到达tomcat之后, 到达servlet之前, tomcat对request请求强行使用iso-8859-1进行了解码)

(2.3 有什么办法阻止tomcat对request请求强行iso-8859-1解码呢?

apache-tomcatconfserver.xml中添加URIEncoding="UTF-8"配置即可,还是来个图吧)

 

3) 在后台(servlet)接收字符串(string2)时毫无疑问的乱码了。

) 这时需要将接收到的字符串(string2)根据iso-8859-1编码重新转换为byte流。再将byte流根据utf-8编码重新解码为字符串(sting3)。

5) 这时的字符串(string3)和前台的字符串(string1)是对应同一个二进制流,并且使用的是同一种编码。也就不会乱码了。

乱码的另一种解决办法:

request.setCharacterEncoding("UTF-8"),这句话熟悉么,这句话的意思是:用"utf-8"编码对客户端的请求进行重新解码。

在步骤2之后(或步骤3中)执行,那么接收到的参数也不会乱码啦。 

一个小例子:

import java.io.UnsupportedEncodingException;

public class ConvertEncodingFormat {

  
  public static String convertEncodingFormat(String str, String formatFrom, String FormatTo) {
    String result = null;
    if (!(str == null || str.length() == 0)) {
      try {
 result = new String(str.getBytes(formatFrom), FormatTo);
      } catch (UnsupportedEncodingException e) {
 e.printStackTrace();
      }
    }
    return result;
  }

  
  public static void main(String[] args) {
     // utf-8编码
    String str = "你好,少年!";

    // UTF-8编码的byte流强行用iso-8859-1解码,毫无疑问的乱码了
    String str1 = convertEncodingFormat(str, "UTF-8", "iso-8859-1");
    System.out.println(str1);

    // 将str1再转化为byte流,重新用UTF-8解码,乱码问题解决
    String str2 = convertEncodingFormat(str1, "iso-8859-1", "UTF-8");
    System.out.println(str2);
  }

}

java字符串的各种编码转换

import java.io.UnsupportedEncodingException; 
 
 
public class ChangeCharset { 
  
 public static final String US_ASCII = "US-ASCII"; 
 
  
 public static final String ISO_8859_1 = "ISO-8859-1"; 
 
  
 public static final String UTF_8 = "UTF-8"; 
 
  
 public static final String UTF_16BE = "UTF-16BE"; 
 
  
 public static final String UTF_16LE = "UTF-16LE"; 
 
  
 public static final String UTF_16 = "UTF-16"; 
 
  
 public static final String GBK = "GBK"; 
 
  
 public String toASCIi(String str) throws UnsupportedEncodingException{ 
 return this.changeCharset(str, US_ASCII); 
 } 
  
 public String toISO_8859_1(String str) throws UnsupportedEncodingException{ 
 return this.changeCharset(str, ISO_8859_1); 
 } 
  
 public String toUTF_8(String str) throws UnsupportedEncodingException{ 
 return this.changeCharset(str, UTF_8); 
 } 
  
 public String toUTF_16BE(String str) throws UnsupportedEncodingException{ 
 return this.changeCharset(str, UTF_16BE); 
 } 
  
 public String toUTF_16LE(String str) throws UnsupportedEncodingException{ 
 return this.changeCharset(str, UTF_16LE); 
 } 
  
 public String toUTF_16(String str) throws UnsupportedEncodingException{ 
 return this.changeCharset(str, UTF_16); 
 } 
  
 public String toGBK(String str) throws UnsupportedEncodingException{ 
 return this.changeCharset(str, GBK); 
 } 
  
  
 public String changeCharset(String str, String newCharset) 
  throws UnsupportedEncodingException { 
 if (str != null) { 
  //用默认字符编码解码字符串。 
  byte[] bs = str.getBytes(); 
  //用新的字符编码生成字符串 
  return new String(bs, newCharset); 
 } 
 return null; 
 } 
  
 public String changeCharset(String str, String oldCharset, String newCharset) 
  throws UnsupportedEncodingException { 
 if (str != null) { 
  //用旧的字符编码解码字符串。解码可能会出现异常。 
  byte[] bs = str.getBytes(oldCharset); 
  //用新的字符编码生成字符串 
  return new String(bs, newCharset); 
 } 
 return null; 
 } 
 
 public static void main(String[] args) throws UnsupportedEncodingException { 
 ChangeCharset test = new ChangeCharset(); 
 String str = "This is a 中文的 String!"; 
 System.out.println("str: " + str); 
 String gbk = test.toGBK(str); 
 System.out.println("转换成GBK码: " + gbk); 
 System.out.println(); 
 String ascii = test.toASCIi(str); 
 System.out.println("转换成US-ASCII码: " + ascii); 
 gbk = test.changeCharset(ascii,ChangeCharset.US_ASCII, ChangeCharset.GBK); 
 System.out.println("再把ASCII码的字符串转换成GBK码: " + gbk); 
 System.out.println(); 
 String iso88591 = test.toISO_8859_1(str); 
 System.out.println("转换成ISO-8859-1码: " + iso88591); 
 gbk = test.changeCharset(iso88591,ChangeCharset.ISO_8859_1, ChangeCharset.GBK); 
 System.out.println("再把ISO-8859-1码的字符串转换成GBK码: " + gbk); 
 System.out.println(); 
 String utf8 = test.toUTF_8(str); 
 System.out.println("转换成UTF-8码: " + utf8); 
 gbk = test.changeCharset(utf8,ChangeCharset.UTF_8, ChangeCharset.GBK); 
 System.out.println("再把UTF-8码的字符串转换成GBK码: " + gbk); 
 System.out.println(); 
 String utf16be = test.toUTF_16BE(str); 
 System.out.println("转换成UTF-16BE码:" + utf16be); 
 gbk = test.changeCharset(utf16be,ChangeCharset.UTF_16BE, ChangeCharset.GBK); 
 System.out.println("再把UTF-16BE码的字符串转换成GBK码: " + gbk); 
 System.out.println(); 
 String utf16le = test.toUTF_16LE(str); 
 System.out.println("转换成UTF-16LE码:" + utf16le); 
 gbk = test.changeCharset(utf16le,ChangeCharset.UTF_16LE, ChangeCharset.GBK); 
 System.out.println("再把UTF-16LE码的字符串转换成GBK码: " + gbk); 
 System.out.println(); 
 String utf16 = test.toUTF_16(str); 
 System.out.println("转换成UTF-16码:" + utf16); 
 gbk = test.changeCharset(utf16,ChangeCharset.UTF_16LE, ChangeCharset.GBK); 
 System.out.println("再把UTF-16码的字符串转换成GBK码: " + gbk); 
 String s = new String("中文".getBytes("UTF-8"),"UTF-8"); 
 System.out.println(s); 
 } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/140196.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号