J2ME URLEncoder UTF-8

本文介绍了一种针对J2ME平台的URL编码方法,该方法支持UTF-8编码,并详细展示了如何处理特殊字符及不同Unicode范围内的字符转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 使用方法
  1. String para = "?userName=" + URLEncoder.encode(userName) + "&password="
  2.         + URLEncoder.encode(password) + "&bookName="
  3.         + URLEncoder.encode(bookName) + "&Form=" + From;

源码:


    1. /**
    2.      * J2ME URLENCODER UTF-8 version
    3.      * 
    4.      * ref - http://blog.youkuaiyun.com/highkay/archive/2008/11/26/3379952.aspx
    5.      * 
    6.      * @param value
    7.      * @return
    8.      */
    9.     public static String encodeUTF8(String value) {
    10.         try {
    11.             int strlen = value.length();
    12.             StringBuffer out = new StringBuffer();
    13.             for (int i = 0; i < strlen; i++) {
    14.                 char t = value.charAt(i);
    15.                 int c = 0;
    16.                 c |= (t & 0xffff);
    17.                 if (c >= 0 && c < 0x80) {
    18.                     switch (t) {
    19.                     case '=':
    20.                         out.append("%3d");
    21.                     break;
    22.                     case ' ':
    23.                         out.append("%20");
    24.                     break;
    25.                     case '+':
    26.                         out.append("%2b");
    27.                     break;
    28.                     case '/'':
    29.                         out.append("%27");
    30.                     break;
    31.                     case '/':
    32.                         out.append("%2F");
    33.                     break;
    34.                     case '.':
    35.                         out.append("%2E");
    36.                     break;
    37.                     case '<':
    38.                         out.append("%3c");
    39.                     break;
    40.                     case '>':
    41.                         out.append("%3e");
    42.                     break;
    43.                     case '#':
    44.                         out.append("%23");
    45.                     break;
    46.                     case '%':
    47.                         out.append("%25");
    48.                     break;
    49.                     case '&':
    50.                         out.append("%26");
    51.                     break;
    52.                     case '{':
    53.                         out.append("%7b");
    54.                     break;
    55.                     case '}':
    56.                         out.append("%7d");
    57.                     break;
    58.                     case '//':
    59.                         out.append("%5c");
    60.                     break;
    61.                     case '^':
    62.                         out.append("%5e");
    63.                     break;
    64.                     case '~':
    65.                         out.append("%73");
    66.                     break;
    67.                     case '[':
    68.                         out.append("%5b");
    69.                     break;
    70.                     case ']':
    71.                         out.append("%5d");
    72.                     break;
    73.                     default:
    74.                         out.append(t);
    75.                         break;
    76.                     }
    77.                 }
    78.                 else if (c > 0x7f && c < 0x800) {
    79.                     out.append("%");
    80.                     out.append(byteArrayToHexString(new byte[] { (byte) (((c >>> 6) & 0x1f) | 0xc0) }));
    81.                     out.append("%");
    82.                     out.append(byteArrayToHexString(new byte[] { (byte) (((c >>> 0) & 0x3f) | 0x80) }));
    83.                 } else if (c > 0x7ff && c < 0x10000) {
    84.                     out.append("%");
    85.                     out.append(byteArrayToHexString(new byte[] { (byte) (((c >>> 12) & 0x0f) | 0xe0) }));
    86.                     out.append("%");
    87.                     out.append(byteArrayToHexString(new byte[] { (byte) (((c >>> 6) & 0x3f) | 0x80) }));
    88.                     out.append("%");
    89.                     out.append(byteArrayToHexString(new byte[] { (byte) (((c >>> 0) & 0x3f) | 0x80) }));
    90.                 } else if (c > 0x00ffff && c < 0xfffff) {
    91.                     out.append("%");
    92.                     out.append(byteArrayToHexString(new byte[] { (byte) (((c >>> 18) & 0x07) | 0xf0) }));
    93.                     out.append("%");
    94.                     out.append(byteArrayToHexString(new byte[] { (byte) (((c >>> 12) & 0x3f) | 0x80) }));
    95.                     out.append("%");
    96.                     out.append(byteArrayToHexString(new byte[] { (byte) (((c >>> 6) & 0x3f) | 0x80) }));
    97.                     out.append("%");
    98.                     out.append(byteArrayToHexString(new byte[] { (byte) (((c >>> 0) & 0x3f) | 0x80) }));
    99.                 }
    100.             }
    101.             return out.toString();
    102.         } catch (Exception ex) {
    103.             ex.printStackTrace();
    104.         }
    105.         return "";
    106.     }
    /**
  1.      * 
  2.      * Convert a byte[] array to readable string format. This makes the "hex"
  3.      * readable!
  4.      * 
  5.      * @return result String buffer in String format
  6.      * 
  7.      * @param in
  8.      *            byte[] buffer to convert to string format
  9.      */
  10.     static String byteArrayToHexString(byte in[]) {
  11.         byte ch = 0x00;
  12.         int i = 0;
  13.         if (in == null || in.length <= 0)
  14.             return null;
  15.         String pseudo[] = { "0""1""2""3""4""5""6""7""8""9",
  16.                 "A""B""C""D""E""F" };
  17.         StringBuffer out = new StringBuffer(in.length * 2);
  18.         while (i < in.length) {
  19.             ch = (byte) (in[i] & 0xF0); // Strip off high nibble
  20.             ch = (byte) (ch >>> 4);
  21.             // shift the bits down
  22.             ch = (byte) (ch & 0x0F);
  23.             // must do this is high order bit is on!
  24.             out.append(pseudo[(int) ch]); // convert the nibble to a String
  25.                                             // Character
  26.             ch = (byte) (in[i] & 0x0F); // Strip off low nibble
  27.             out.append(pseudo[(int) ch]); // convert the nibble to a String
  28.                                             // Character
  29.             i++;
  30.         }
  31.         String rslt = new String(out);
  32.         return rslt;
  33.     }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值