A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© LiuKang 中级黑马   /  2013-11-20 22:26  /  1332 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 LiuKang 于 2013-11-20 22:37 编辑

1.在服务器(Struts Action)中将中文转化为Unicode字符编码 2.在手机端J2ME中将Unicode码再转换成中文。
1.服务器端
/**
     * 转换为Unicode字符  并输出
     * @param servletOutputStream  ( response.getOutputStream() )
     * @param value   (传输的值)
     */
    public static void writeUnicode(final ServletOutputStream servletOutputStream,  final String value) {
        try {
            final String unicode = gbEncoding(value);
            final byte[] data = unicode.getBytes();
            servletOutputStream.write(data); // 然后写出转化后的字符串
        } catch (IOException e) {

        }
    }
    public static String gbEncoding(final String gbString) {
        char[] utfBytes = gbString.toCharArray();
        String unicodeBytes = "";
        for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {
            String hexB = Integer.toHexString(utfBytes[byteIndex]);
            if (hexB.length() <= 2) {
                hexB = "00" + hexB;
            }
            unicodeBytes = unicodeBytes + "\\\\u" + hexB;
        }
        //System.out.println("unicodeBytes is: " + unicodeBytes);
        return unicodeBytes;
    }


    public ActionForward TestU(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) {
        try {
            this.writeUnicode(response.getOutputStream(), "齐鲁软件设计大赛济南职业学院");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
2.客户端
private void con() {
        HttpConnection hc;
        try {
            hc = (HttpConnection) Connector
                    .open("http://localhost:8080/stu/roomReadPhone.do?meth=TestU");

            InputStream is = hc.openInputStream();
            String content = "";
            while (true) {
                int ch = is.read();
                if (ch == -1) {
                    break;
                }
                content += (char) ch;
            }


            String str = new String(content.getBytes("ISO8859-1"), "GBK");

            // #style messageAlert
            Alert alert = new Alert("", this.decodeUnicode(str), null, AlertType.INFO);
            alert.setTimeout(Alert.FOREVER);
            midlet.getDisplay().setCurrent(alert);
            
        } catch (IOException e) {

            System.out.println(e + "kkkk");

        }

    }

    //反编为中文
    private String decodeUnicode(final String dataStr) {
        
        final StringBuffer buffer = new StringBuffer();
        
        String[] s = this.split(dataStr, "\\\\u");
        for(int i = 1; i < s.length; i++){
            
            String st = s;
            
            if(i==s.length-1){
                st = s.substring(2);
            }
            
            char c = (char) Integer.parseInt(st, 16); // 16进制parse整形字符串。
            System.out.println("dd:" + c);
            buffer.append(new Character(c).toString());
        
        }
        return buffer.toString();
    }
   
   
   

    /**
    *  J2me中无Split分割字符串操作,  自定义方法 有点小Bug  嘿嘿
     * 以字符串中的特定字符为标志对字符串进行分割,返回分割后的字符串数组
     *
     * @param original
     *            被分割的字符串
     * @param regex
     *            分割标志
     * @return 被分割后的字符串数组
     */
    public  String[] split(String original, String regex) {

        int startIndex = 0;

        Vector v = new Vector();

        String[] str = null;
        // 存储取子串时起始位置
        int index = 0;

        // 获得匹配子串的位置
        startIndex = original.indexOf(regex);
        // 如果起始字符串的位置小于字符串的长度,则证明没有取到字符串末尾。
        // -1代表取到了末尾
        while (startIndex < original.length() && startIndex != -1) {

            String temp = original.substring(index, startIndex);
            // 取子串
            v.addElement(temp);

            // 设置取子串的起始位置
            index = startIndex + regex.length();

            // 获得匹配子串的位置
            startIndex = original.indexOf(regex, startIndex + regex.length());
        }

        // 取结束的子串
        v.addElement(original.substring(index + 1 - regex.length()));
        // Vector对象转换成数组
        str = new String[v.size()];
        for (int i = 0; i < str.length; i++) {
            str = (String) v.elementAt(i);
        }
        // 返回生成的数组
        return str;
    }// End Of split(String, String)

评分

参与人数 1技术分 +1 收起 理由
FFF + 1 山寨、鼓励新人提问再回答问题~下次表山寨.

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马