奇数个汉字GBK编码,转UTF-8编码/*模拟测试*/public class testString {public static void main(String[] args) throws UnsupportedEncodingException {String str1 = "10月.jpg";//假设是过滤器(这个是项目全工程中公用的类,不
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/03 11:42:01
![奇数个汉字GBK编码,转UTF-8编码/*模拟测试*/public class testString {public static void main(String[] args) throws UnsupportedEncodingException {String str1 =](/uploads/image/z/14817726-54-6.jpg?t=%E5%A5%87%E6%95%B0%E4%B8%AA%E6%B1%89%E5%AD%97GBK%E7%BC%96%E7%A0%81%2C%E8%BD%ACUTF-8%E7%BC%96%E7%A0%81%2F%2A%E6%A8%A1%E6%8B%9F%E6%B5%8B%E8%AF%95%2A%2Fpublic+class+testString+%7Bpublic+static+void+main%28String%5B%5D+args%29+throws+UnsupportedEncodingException+%7BString+str1+%3D+%2210%E6%9C%88.jpg%22%3B%2F%2F%E5%81%87%E8%AE%BE%E6%98%AF%E8%BF%87%E6%BB%A4%E5%99%A8%EF%BC%88%E8%BF%99%E4%B8%AA%E6%98%AF%E9%A1%B9%E7%9B%AE%E5%85%A8%E5%B7%A5%E7%A8%8B%E4%B8%AD%E5%85%AC%E7%94%A8%E7%9A%84%E7%B1%BB%2C%E4%B8%8D)
奇数个汉字GBK编码,转UTF-8编码/*模拟测试*/public class testString {public static void main(String[] args) throws UnsupportedEncodingException {String str1 = "10月.jpg";//假设是过滤器(这个是项目全工程中公用的类,不
奇数个汉字GBK编码,转UTF-8编码
/*模拟测试*/
public class testString {
public static void main(String[] args) throws UnsupportedEncodingException {
String str1 = "10月.jpg";
//假设是过滤器(这个是项目全工程中公用的类,不允许修改)
try {
str1 = new String(str1.getBytes("UTF-8"),"GBK");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(str1);
//假设是我处理的Action(进行编码转回)
try {
str1 = new String(str1.getBytes("GBK"),"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(str1);
}
}
输出结果:
10链�jpg
10�?jpg
检验发现,如果是偶数个汉字“10月份.jpg”
输出结果:
10链堜唤.jpg
10月份.jpg
合作开发的工程,尽量不要修改过滤器,能在Action我想尽量在Action中处理.
想了好久没想出处理方法,百度上也未搜索到合理的处理方案,
奇数个汉字GBK编码,转UTF-8编码/*模拟测试*/public class testString {public static void main(String[] args) throws UnsupportedEncodingException {String str1 = "10月.jpg";//假设是过滤器(这个是项目全工程中公用的类,不
str1 = new String(str1.getBytes("UTF-8"),"GBK");
按你的测试过程,这一步已经丢字符了,出现问号了.
--------------------------------
如果接受到的数据,是UTF-8编码的字节流,那么应该按UTF-8还原为字符,然后再getBytes("GBK")转为GBK字节流~