没有找到合适的产品?
联系客服协助选型:023-68661681
提供3000多款全球软件/控件产品
针对软件研发的各个阶段提供专业培训与技术咨询
根据客户需求提供定制化的软件开发服务
全球知名设计软件,显著提升设计质量
打造以经营为中心,实现生产过程透明化管理
帮助企业合理产能分配,提高资源利用率
快速打造数字化生产线,实现全流程追溯
生产过程精准追溯,满足企业合规要求
以六西格玛为理论基础,实现产品质量全数字化管理
通过大屏电子看板,实现车间透明化管理
对设备进行全生命周期管理,提高设备综合利用率
实现设备数据的实时采集与监控
利用数字化技术提升油气勘探的效率和成功率
钻井计划优化、实时监控和风险评估
提供业务洞察与决策支持实现数据驱动决策
                
            原创|其它|编辑:郝浩|2009-09-25 10:59:02.000|阅读 620 次
概述:本文介绍Java ME平台上的URLEncoder实现类,它是从java.net.URLEncoder修改而来,能够正常完成URL编码的工作。使用的时候直接调用URLEncoder.encode("中国")即可如果向服务器端发送。在服务器端以servlet为例request.getParameter("para")即可获得“参数”。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
这个类是从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="//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). 
* 
*/ 
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= 
* "//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 != null) 
writer.write(c); 
/* 
* If this character represents the start of a Unicode 
* surrogate pair, then pass in two characters. It's not 
* clear what should be done if a bytes reserved in the 
* surrogate pairs range occurs outside of a legal 
* surrogate pair. For now, just treat it as if it were 
* any other character. 
*/ 
if (c >= 0xD800 && c <= 0xDBFF) { 
/* 
System.out.println(Integer.toHexString(c) 
+ " is high surrogate"); 
*/ 
if ( (i+1) < s.length()) { 
int d = (int) s.charAt(i+1); 
/* 
System.out.println("\tExamining " 
+ Integer.toHexString(d)); 
*/ 
if (d >= 0xDC00 && d <= 0xDFFF) { 
/* 
System.out.println("\t" 
+ Integer.toHexString(d) 
+ " is low surrogate"); 
*/ 
writer.write(d); 
i++; 
} 
} 
} 
writer.flush(); 
} catch(IOException e) { 
buf.reset(); 
continue; 
} 
byte[] ba = buf.toByteArray(); 
for (int j = 0; j < ba.length; j++) { 
out.append('%'); 
char ch = forDigit((ba[j] >> 4) & 0xF, 16); 
if (isLetter(ch)) { 
ch -= caseDiff; 
} 
out.append(ch);
ch = forDigit((ba[j] & 0xF), 16); 
//ch = forDigit(ba[j] & 0xF, 16); 
if (isLetter(ch)) { 
ch -= caseDiff; 
} 
out.append(ch); 
} 
buf.reset(); 
needToChange = true; 
} 
}
return (needToChange? out.toString() : s); 
}
private static boolean isLetter(char c){ 
if( (c >= 'a' && c <= 'z') || (c >='A' && c <= 'Z')) 
return true; 
return false; 
}
private static char forDigit(int digit,int radix){ 
if ((digit >= radix) || (digit < 0)) { 
return '\0';        } 
if ((radix < MIN_RADIX) || (radix > MAX_RADIX)) { 
return '\0'; 
} 
if (digit < 10) { 
return (char)('0' + digit); 
} 
return (char)('a' - 10 + digit); 
} 
}
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@fz165y.cn
文章转载自:IT专家网



					接DevExpress原厂商通知,将于近日上调旗下产品授权价格,现在下单客户可享受优惠报价!
					面对“数字中国”建设和中国制造2025战略实施的机遇期,中车信息公司紧跟时代的步伐,以“集约化、专业化、标准化、精益化、一体化、平台化”为工作目标,大力推进信息服务、工业软件等核心产品及业务的发展。在慧都3D解决方案的实施下,清软英泰建成了多模型来源的综合轻量化显示平台、实现文件不失真的百倍压缩比、针对模型中的大模型文件,在展示平台上进行流畅展示,提升工作效率,优化了使用体验。
					本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
					本站的模型资源均免费下载,登录后即可下载。模型仅供学习交流,勿做商业用途。
服务电话
重庆/ 023-68661681
华东/ 13452821722
华南/ 18100878085
华北/ 17347785263
客户支持
技术支持咨询服务
服务热线:400-700-1020
邮箱:sales@fz165y.cn
关注我们
地址 : 重庆市九龙坡区火炬大道69号6幢
                
            
星空最火知名网站