快速业务通道

Java ME平台中的URLEncoder实现类 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-25

Java ME平台中的URLEncoder实现类

时间:2007-05-30 j2medev

这个类是从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''

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号