图文验证码模块乱码问题的解决

来源:互联网 发布:aria2 百度网盘windows 编辑:程序博客网 时间:2024/06/07 04:03

图文验证码模块,代码就不多说了,百度一下,都有资源下载。http://hehongwei44.iteye.com/blog/834837 这篇文字对图文验证码有了详细的介绍。但是,在实际操作过程中,会发现中文乱码问题。抽取其中的主要方法,作为一个java类,控制台打印出来的也是乱码。测试类代码如下

test.java

package test;


import java.io.UnsupportedEncodingException;
import java.util.Random;


public class test {


public static void main(String[] args) {
// TODO Auto-generated method stub


Random random=new Random ();
String ctmp = "";
String[] rBase={"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};   
        //生成第一位区码  
        int r1=random.nextInt(3)+11;  
        String str_r1=rBase[r1];  
        //生成第二位区码  
        int r2;  
        if(r1==13){  
            r2=random.nextInt(7);     
        }else{  
            r2=random.nextInt(16);  
        }  
        String str_r2=rBase[r2];  
        //生成第一位位码  
        int r3=random.nextInt(6)+10;  
        String str_r3=rBase[r3];  
        //生成第二位位码  
        int r4;  
        if(r3==10){  
            r4=random.nextInt(15)+1;  
        }else if(r3==15){  
            r4=random.nextInt(15);  
        }else{  
            r4=random.nextInt(16);  
        }  
        String str_r4=rBase[r4];  
        //将生成的机内码转换为汉字  
        byte[] bytes=new byte[2];  
        //将生成的区码保存到字节数组的第一个元素中  
        String str_12=str_r1+str_r2;  
        int tempLow=Integer.parseInt(str_12, 16);  
        bytes[0]=(byte) tempLow;  
        //将生成的位码保存到字节数组的第二个元素中  
        String str_34=str_r3+str_r4;  
        int tempHigh=Integer.parseInt(str_34, 16);  
        bytes[1]=(byte)tempHigh;  
//        try {
// ctmp=new String(bytes,"GBK");                                                  //将这几行注释打开
// } catch (UnsupportedEncodingException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
//
 
        ctmp=new String(bytes); //将这行注释
System.out.println("生成汉字:" + ctmp);




}


}

将上面的注释打开,并且将 ctmp=new String(bytes);这句代码进行注释,问题就能解决。问题解决了在这里给大家展示下:


如果不能完美解决大家的问题,欢迎一起来探讨。

0 0
原创粉丝点击