当前位置:主页   - 电脑 - 程序设计 - JAVA
Java ME平台中的URLEncoder实现类
来源:网络   作者:   更新时间:2012-06-09
收藏此页】    【字号    】    【打印】    【关闭

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

String data = "para="+URLEncoder.encode("参数");http://tech.ddvip.com/2008-11/
outputStream.write(data.getBytes());http://tech.ddvip.com/2008-11/
.......

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

package com.j2medev.httpme.tools;http://tech.ddvip.com/2008-11/
import java.io.ByteArrayOutputStream;http://tech.ddvip.com/2008-11/
import java.io.IOException;http://tech.ddvip.com/2008-11/
import java.io.OutputStreamWriter;http://tech.ddvip.com/2008-11/
import java.io.UnsupportedEncodingException;http://tech.ddvip.com/2008-11/
/**http://tech.ddvip.com/2008-11/
* Utility class for form encoding.this class is modified form java.net.URLEncoderhttp://tech.ddvip.com/2008-11/
so that it can work well in cldc env.http://tech.ddvip.com/2008-11/
* This class contains static methodshttp://tech.ddvip.com/2008-11/
* for converting a String to the <CODE>application/x-www-form-urlencoded</CODE> MIMEhttp://tech.ddvip.com/2008-11/
* format. For more information about HTML form encoding, consult the HTMLhttp://tech.ddvip.com/2008-11/
* <A HREF="http://www.w3.org/TR/html4/">specification</A>.http://tech.ddvip.com/2008-11/
*http://tech.ddvip.com/2008-11/
* <p>http://tech.ddvip.com/2008-11/
* When encoding a String, the following rules apply:http://tech.ddvip.com/2008-11/
*http://tech.ddvip.com/2008-11/
* <p>http://tech.ddvip.com/2008-11/
* <ul>http://tech.ddvip.com/2008-11/
* <li>The alphanumeric characters "<code>a</code>" throughhttp://tech.ddvip.com/2008-11/
*   "<code>z</code>", "<code>A</code>" throughhttp://tech.ddvip.com/2008-11/
*   "<code>Z</code>" and "<code>0</code>"http://tech.ddvip.com/2008-11/
*   through "<code>9</code>" remain the same.http://tech.ddvip.com/2008-11/
* <li>The special characters "<code>.</code>",http://tech.ddvip.com/2008-11/
*   "<code>-</code>", "<code>*</code>", andhttp://tech.ddvip.com/2008-11/
*   "<code>_</code>" remain the same.http://tech.ddvip.com/2008-11/
* <li>The space character "<code> </code>" ishttp://tech.ddvip.com/2008-11/
*   converted into a plus sign "<code>+</code>".http://tech.ddvip.com/2008-11/
* <li>All other characters are unsafe and are first converted intohttp://tech.ddvip.com/2008-11/
*   one or more bytes using some encoding scheme. Then each byte ishttp://tech.ddvip.com/2008-11/
*   represented by the 3-character stringhttp://tech.ddvip.com/2008-11/
*   "<code>%<i>xy</i></code>", where <i>xy</i> is thehttp://tech.ddvip.com/2008-11/
*   two-digit hexadecimal representation of the byte.http://tech.ddvip.com/2008-11/
*   The recommended encoding scheme to use is UTF-8. However,http://tech.ddvip.com/2008-11/
*   for compatibility reasons, if an encoding is not specified,http://tech.ddvip.com/2008-11/
*   then the default encoding of the platform is used.http://tech.ddvip.com/2008-11/
* </ul>http://tech.ddvip.com/2008-11/
*http://tech.ddvip.com/2008-11/
* <p>http://tech.ddvip.com/2008-11/
* For example using UTF-8 as the encoding scheme the string "Thehttp://tech.ddvip.com/2008-11/
* string ü@foo-bar" would get converted tohttp://tech.ddvip.com/2008-11/
* "The+string+%C3%BC%40foo-bar" because in UTF-8 the characterhttp://tech.ddvip.com/2008-11/
* ü is encoded as two bytes C3 (hex) and BC (hex), and thehttp://tech.ddvip.com/2008-11/
* character @ is encoded as one byte 40 (hex).http://tech.ddvip.com/2008-11/
*http://tech.ddvip.com/2008-11/
*/http://tech.ddvip.com/2008-11/
public class URLEncoder {http://tech.ddvip.com/2008-11/
/** The characters which do not need to be encoded. */http://tech.ddvip.com/2008-11/
private static boolean[] dontNeedEncoding;http://tech.ddvip.com/2008-11/
private static String defaultEncName = "";http://tech.ddvip.com/2008-11/
static final int caseDiff = ('a' - 'A');http://tech.ddvip.com/2008-11/
static {http://tech.ddvip.com/2008-11/
dontNeedEncoding = new boolean[256];http://tech.ddvip.com/2008-11/
int i;http://tech.ddvip.com/2008-11/
for (i = 'a'; i <= 'z'; i++) {http://tech.ddvip.com/2008-11/
dontNeedEncoding[i] = true;http://tech.ddvip.com/2008-11/
}http://tech.ddvip.com/2008-11/
for (i = 'A'; i <= 'Z'; i++) {http://tech.ddvip.com/2008-11/
dontNeedEncoding[i] = true;http://tech.ddvip.com/2008-11/
}http://tech.ddvip.com/2008-11/
for (i = '0'; i <= '9'; i++) {http://tech.ddvip.com/2008-11/
dontNeedEncoding[i] = true;http://tech.ddvip.com/2008-11/
}http://tech.ddvip.com/2008-11/
dontNeedEncoding[' '] = true; // encoding a space to a + is donehttp://tech.ddvip.com/2008-11/
in the encode() methodhttp://tech.ddvip.com/2008-11/
dontNeedEncoding['-'] = true;http://tech.ddvip.com/2008-11/
dontNeedEncoding['_'] = true;http://tech.ddvip.com/2008-11/
dontNeedEncoding['.'] = true;http://tech.ddvip.com/2008-11/
dontNeedEncoding['*'] = true;http://tech.ddvip.com/2008-11/
defaultEncName = System.getProperty("microedition.encoding");http://tech.ddvip.com/2008-11/
if(defaultEncName == null

其它资源
来源声明

版权与免责声明
1、本站所发布的文章仅供技术交流参考,本站不主张将其做为决策的依据,浏览者可自愿选择采信与否,本站不对因采信这些信息所产生的任何问题负责。
2、本站部分文章来源于网络,其版权为原权利人所有。由于来源之故,有的文章未能获得作者姓名,署“未知”或“佚名”。对于这些文章,有知悉作者姓名的请告知本站,以便及时署名。如果作者要求删除,我们将予以删除。除此之外本站不再承担其它责任。
3、本站部分文章来源于本站原创,本站拥有所有权利。
4、如对本站发布的信息有异议,请联系我们,经本站确认后,将在三个工作日内做出修改或删除处理。
请参阅权责声明