文章分类 | 推荐文章 | 最新文章 | 热点文章 | 最新软件 | 精品软件 | 下载排行 | 推荐下载 | 免费看大片 | WPS | 杀毒软件
清风网络
首 页 软件下载 网络学院 数码学院
QQ 电脑入门 游戏 操作系统 图形处理 办公软件 媒体动画 精文荟萃 工具软件 网络编程 程序开发 网络技术 认证考试 网站建设 文章专栏
当前位置:清风网络学院网络编程J2EE/J2MEJava ME平台中的URLEncoder实现类
精品推荐
特别推荐
·J2EE Web开发技术期待一次新的技术变革
·J2ME程序开发初学者快速入门的九大要点
·使用技巧:J2ME中程序优化的十个小方法
·RMS从入门到精通之一
·J2EE应用程序中SQL语句的自动构造方法
·解决J2EE系统应用性能问题常用优化项目
·J2EE实用技巧:提升JSP应用程序的绝招
·J2ME内存优
·J2ME简介
·J2EE技术
热点TOP10
·JBoss 文档(三) JBoss和JMS
·基于MIDP1.0实现通信录
·3D数学知识简介
·第一个Spring MVC程序
·配置Eclipse进行远程调试
·jBPM实例化一个流程
·JBoss4.0.2集群指南
·基于J2EE的Blog平台
·EJB 3.0简介
·FC API(JSR 75)简单讲解
·介绍J2ME可选包WMA(JSR120)
·MIDP终端模拟之一:一个简单的模拟器MIDlet
·使用platformRequest()自动更新MIDlet套件
·Spring 系列:进入 Spring MVC
·从自定义字节数组创建图片
·调整压力测试工具
·基于Java的Web服务器工作原理(三)
·实例-用JSF实现文件下载
·经典飞机游戏代码S60
·Spring 系列:Spring JMS 消息处理

Java ME平台中的URLEncoder实现类

日期:2007年5月11日 作者: 查看:[大字体 中字体 小字体]


这个类是从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() { }
   
   
    /**
     * Translates a string into "<CODE>x-www-form-urlencoded</CODE>"
     * format.This method uses the platform's default encoding
     * as the encoding scheme to obtain the bytes for unsafe characters.
     *
     * @param  s the string to be translated.
     *
     * @return The resulting string.
     */
    public static String encode(String s) {
        String str = null;
        str = encode(s, defaultEncName);
        return str;
    }
       /**
     * Translates a string into <code>application/x-www-form-urlencoded</code>
     * format using a specific encoding scheme. This method uses the
     * supplied encoding scheme to obtain the bytes for unsafe
     * characters.
     * <p>
     * <em><strong>Note:</strong> The <a href=
     * "http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars">
     * World Wide Web Consortium Recommendation</a> states that
     * UTF-8 should be used. Not doing so may introduce
     * incompatibilites.</em>
     *
     * @param   s   <code>String</code> to be translated.
     * @param   enc   The name of a supported character encoding such as UTF-8
     * @return  the translated <code>String</code>.
     */
    public static String encode(String s, String enc) {
       
        boolean needToChange = false;
        boolean wroteUnencodedChar = false;
        int maxBytesPerChar = 10; // rather arbitrary limit, but safe for now
        StringBuffer out = new StringBuffer(s.length());
        ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar);
        OutputStreamWriter writer = null;
        try {
            writer = new OutputStreamWriter(buf, enc);
        } catch (UnsupportedEncodingException ex) {
            try {
                writer = new OutputStreamWriter(buf,defaultEncName);
            } catch (UnsupportedEncodingException e) {
                //never reach
            }
        }
       
        for (int i = 0; i < s.length(); i++) {
            int c = (int) s.charAt(i);
            //System.out.println("Examining character: " + c);
            if (c <256 && dontNeedEncoding[c]) {
                if (c == ' ') {
                    c = '+';
                    needToChange = true;
                }
                //System.out.println("Storing: " + c);
                out.append((char)c);
                wroteUnencodedChar = true;
            } else {
                // convert to external encoding before hex conversion
                try {
                    if (wroteUnencodedChar) { // Fix for 4407610
                        writer = new OutputStreamWriter(buf, enc);
                        wroteUnencodedChar = false;
                    }
                    if(writer !
[1] [2] [3] 下一页 




上一篇:MIDP 2.0安全机制 与 MIDlet 数字签名

下一篇:J2ME游戏开发之用setClip分割图片

Java ME平台中的URLEncoder实现类 相关文章:
·如何实现局域网打印机共享
·Java图形用户界面设计
·TCP/IP编程实现远程文件传输
·Visual C++ 实现数字化图像的分割
·ASP.NET购物车的实现及结算处理
·一个Struts实现分页,增删改查,Tiles,国际化的DEMO
·javascript+xml实现二级下拉菜单,不会被任何标签或元素遮住
·用C语言实现Ping程序功能
·javascript 常用代码大全
·JavaScript经典效果集锦
Java ME平台中的URLEncoder实现类 相关软件:
·Thinking In Java 英文版
·TCP-IP详解卷2:实现
· JavaScript 语言参考 中文版(CHM)
·Javascript高级教程
·张效祥javascript视频教程 lesson67附教程
·Java 2 入门与实例教程(PDG)
·JavaScript 使用详解
·Effective Java 中文版
·Java 教程及实例
·DJ Java DecompilerV3.9.9.91

特别声明:本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
[打印本页] [关闭窗口] 转载请注明来源:http://www.vipcn.net
| 帮助(?) | 版权声明 | 友情连接 | 关于我们 | 信息发布
Copyright 2007 www.vipcn.net All Rights Reserved. 鄂ICP备05000083号Powered by:viphot