当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:16:00  来源:本站整理

Java ME平台中的URLEncoder实现类[Java编程]

赞助商链接



  本文“Java ME平台中的URLEncoder实现类[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

这个类是从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() { }
  以上是“Java ME平台中的URLEncoder实现类[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:

  • 利用Javascript实现网页水印(非图片水印)
  • Java开辟环境的搭建
  • Ubuntu java安装与配置
  • 办理Ubuntu 10.04 Firefox3.6 Java浏览器插件不工作的问
  • Ubuntu重装后Java环境的设置
  • Sun Java进入Ubuntu 10.10软件中央
  • Ubuntu 10.10配置Java开辟环境
  • 在Ubuntu 10.10中配置Java环境变量的办法
  • Ubuntu下Java环境的搭建
  • Ubuntu 10.04 下安装 Java, JRE
  • Ubuntu 10.04下的搭建SUN JAVA开辟环境
  • Ubuntu 12.04安装java7
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .