Chinaunix首页 | 论坛 | 博客
  • 博客访问: 273444
  • 博文数量: 757
  • 博客积分: 40040
  • 博客等级: 大将
  • 技术积分: 4935
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 12:37
文章分类

全部博文(757)

文章存档

2011年(1)

2008年(756)

我的朋友

分类:

2008-09-09 12:38:45

    这个类是从java.net.URLEncoder修改来的  经测试能够正常完成URL编码的工作,在几部手机上测试过。使用的时候直接调用URLEncoder.encode("中国")即可  如果向端发送。可以使用如下的办法对中文进行编码,然后发送向。

String data = "para="+URLEncoder.encode("参数");

outputStream.write(data.getBytes());

.......

在服务器端 以servlet为例 request.getParameter("para")即可获得“参数”

package com.j2medev.httpme.tools;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
/**
 * Utility class for  form encoding.this class is modified form java.net.URLEncoder so that it can work well in cldc env.
 * This class contains static methods
 * for converting a String to the application/x-www-form-urlencoded MIME
 * format. For more information about HTML form encoding, consult the HTML
 * specification.
 *
 *


 * When encoding a String, the following rules apply:
 *
 *


 *


     *
  • The alphanumeric characters "a" through
     *     "z", "A" through
     *     "Z" and "0"
     *     through "9" remain the same.
     *
  • The special characters ".",
     *     "-", "*", and
     *     "_" remain the same.
     *
  • The space character " " is
     *     converted into a plus sign "+".
     *
  • All other characters are unsafe and are first converted into
     *     one or more bytes using some encoding scheme. Then each byte is
     *     represented by the 3-character string
     *     "%xy", where xy is the
     *     two-digit hexadecimal representation of the byte.
     *     The recommended encoding scheme to use is UTF-8. However,
     *     for compatibility reasons, if an encoding is not specified,
     *     then the default encoding of the platform is used.
     *

 *
 *


 * For example using UTF-8 as the encoding scheme the string "The
 * string ü@foo-bar" would get converted to
 * "The+string+%C3%BC%40foo-bar" because in UTF-8 the character
 * ü is encoded as two bytes C3 (hex) and BC (hex), and the
 * character @ is encoded as one byte 40 (hex).
 *
 * @author  mingjava
 * @version 0.1 05/06/2006
 * @since   httpme 0.1
 */
public class URLEncoder {
   
    /** The characters which do not need to be encoded. */
    private static boolean[] dontNeedEncoding;
    private static String defaultEncName = "";
    static final int caseDiff = ('a' - 'A');
    static {
        dontNeedEncoding = new boolean[256];
        int i;
        for (i = 'a'; i <= 'z'; i++) {
            dontNeedEncoding[i] = true;
        }
        for (i = 'A'; i <= 'Z'; i++) {
            dontNeedEncoding[i] = true;
        }
        for (i = '0'; i <= '9'; i++) {
            dontNeedEncoding[i] = true;
        }
        dontNeedEncoding[' '] = true; // encoding a space to a + is done in the encode() method
        dontNeedEncoding['-'] = true;
        dontNeedEncoding['_'] = true;
        dontNeedEncoding['.'] = true;
        dontNeedEncoding['*'] = true;
        defaultEncName = System.getProperty("microedition.encoding");
        if(defaultEncName == null || defaultEncName.trim().length() == 0){
            defaultEncName = "UTF-8";
        }
    }
   
    public static final int MIN_RADIX = 2;
   
    /**
     * The maximum radix available for conversion to and from strings.
     */
    public static final int MAX_RADIX = 36;
    /**
     * The class is not meant to be instantiated.
     */
    private URLEncoder() { }
   
 

[1]  

【责编:Peng】

--------------------next---------------------

阅读(85) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~