java开发 中文传参

来源:互联网 发布:php class 编辑:程序博客网 时间:2024/05/17 01:43

在中文传参时,将参数转化为gb2312的字节格式。

 byte[] bytes= 中文.getBytes("gb2312");
 String result = new String();
 for (int i = 0, num = bytes.length; i < num; i++) {
    result = result + bytes[i];
     if (i < num - 1)
        result = result + "_";
    }

return "@"+result +"@";

 

在java类接收到字节格式的参数后,再转化为中文

参数=参数.substring(1, numString.length()-1);
StringTokenizer stk = new StringTokenizer(参数, "_");
byte[] num = new byte[stk.countTokens()];
int index = 0;
while (stk.hasMoreTokens()) {
   num[index++] = Byte.parseByte(stk.nextToken());
}
return new String(num);

 

如此即可。若是在linux上运行该系统,需设置字符集为zh_CN.GB2312。

 

 

原创粉丝点击