Java资源网

| JAVA基础 | 环境配置 | JDBC | 线程技术 | Socket编程 | JavaMail | JAVA与XML | 设计模式 | 技术新闻 | Java认证 | 程序人生 软件下载
| JSP&Servlet | Spring | Struts | Hibernate | JBuilder | Eclipse | WebService | EJB技术 | J2ME开发 | 应用服务器 | JXTA | Ajax
Articles search文章搜索
   关键字:
   类 别:
       
New download 最新下载
· [组件]HTML Parser 1.5
· [教程]WebSphere Studio应用教程
· [组件]JDom 1.0
· [工具]Junit3.8.1
· [教程]EJB编程及J2EE系统架构和设计
· [教程]EJB教程
· [教程]J2EE Tutorial中文版
· [教程]Java编程思想2(英文)
· [教程]java编程思想(完整版)
· [教程]Java网络编程
New articles 最新文章
· 设计移动 Web 服务
· 解析XML的时候完全忽略DTD
· 理解XML Schema XML Schema 初步
· 标签库的深入研究
· 提升JSP应用程序的七大绝招
· 如何使用JDOM对XML文件进行操作
· 处理XML字符串中特殊字符
· 利用Digester把XML转换成为Java对象
· 使用WebService 和RMI远程协作
· 使用Axis开发Web Service程序
Articles top 热门文章
· Eclipse基础--plugin插件安装(6644)
· eclipse+tomcat+lomboz的安装配置说明(4774)
· Java程序员就业前景(4584)
· Windows下JAVA环境变量的设置祥解(3788)
· Tomcat下JSP、Servlet和JavaBean环境的配置(3716)
· 使用links方式安装Eclipse插件(3698)
· 一个老程序员的心理话(3533)
· linux下jdk的安装与配置(3459)
· 初学者入门:Structs中基本配置入门(3334)
· Eclipse 运行命令行参数大全(3084)
您的位置:首页>>J2ME开发>>Java ME平台中的URLEncoder实现类
Java ME平台中的URLEncoder实现类
2007-04-17   来源:www.javaresearch.org  作者:未知

    这个类是从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 <CODE>application/x-www-form-urlencoded</CODE> MIME
 * format. For more information about HTML form encoding, consult the HTML
 * <A HREF="http://www.w3.org/TR/html4/">specification</A>.
 *
 * <p>
 * When encoding a String, the following rules apply:
 *
 * <p>
 * <ul>
 * <li>The alphanumeric characters "<code>a</code>" through
 *     "<code>z</code>", "<code>A</code>" through
 *     "<code>Z</code>" and "<code>0</code>"
 *     through "<code>9</code>" remain the same.
 * <li>The special characters "<code>.</code>",
 *     "<code>-</code>", "<code>*</code>", and
 *     "<code>_</code>" remain the same.
 * <li>The space character "<code> </code>" is
 *     converted into a plus sign "<code>+</code>".
 * <li>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
 *     "<code>%<i>xy</i></code>", where <i>xy</i> 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.
 * </ul>
 *
 * <p>
 * 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() { }
   
 

  --相关文章--
· 详细介绍手机游戏中的声音处理 (2007-04-17)
· 让JavaME程序实现真正Run Anywhere (2007-04-17)
· 端到端J2ME应用开发实例——介绍Smart Ticket (2007-04-17)
· 移动开发谁领风骚 J2ME开发工具面面观 (2007-04-17)
· 用NetBeans平台开发J2ME游戏实例讲解3 (2007-04-17)
· 用NetBeans平台开发J2ME游戏实例讲解2 (2007-04-17)

版权所有©2005-2006 JAVA资源网 渝ICP备05007591号 虚拟主机 | 关于我们 | 联系方式 | 广告业务 | 网站地图 | 友情链接