手机中文字符网络传输的解决方案//the class of read props
来源:互联网 发布:海关数据编码 编辑:程序博客网 时间:2024/05/01 09:15
由于手机端使用的是UTF-8编码,所以在网络传输中需要进行码制转换,无论是从服务器到客户端,还是客户端到服务器端。下面我写出我的解决方法,该方法在Nokia 7610上运行成功。
思路:将中文字符串转换成Unicode编码格式(即”/uXXXX/uXXXX……”形式)的字符串进行网络传输,在接收后反转换成中文字符串,以便在手机上显示或者存到服务器端数据库中,服务器接收和发送使用ISO8859-1编码。
编码:
public static String encodeUnicode(final String gbString)
{
char[] utfBytes = gbString.toCharArray();
String unicodeBytes = "";
for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {
String hexB = Integer.toHexString(utfBytes[byteIndex]);
if (hexB.length() <= 2) { //非中文字符
hexB = "00" + hexB;
}
unicodeBytes = unicodeBytes + "//u"+ hexB;
}
// System.out.println("unicodeBytes is: " + unicodeBytes);
return unicodeBytes;
}
解码:
public static String decodeUnicode(final String dataStr)
{
int start = 0;
int end = 0;
StringBuffer buffer = new StringBuffer();
while (start > -1) {
end = dataStr.indexOf("//u", start + 2);
String charStr = "";
if (end == -1) {
charStr = dataStr.substring(start + 2, dataStr.length());
} else {
charStr = dataStr.substring(start + 2, end);
}
char letter = (char) Integer.parseInt(charStr, 16); // 16进制整形字符串。
buffer.append(new Character(letter).toString());
start = end;
}
return buffer.toString();
}
示例:客户端发送注册信息到服务器,存储到数据库中。(服务器到客户端处理是一样的)
客户端(使用url encode):从文本框中接收中文字符,编码使用POST方式连接服务器。
TextField yourName = null;
StringBuffer oBuf = new StringBuffer();
………
//name
String temp = yourName.getString().trim();
System.out.println("Get textfield name :" + temp );
temp = encodeUnicode (temp);
System.out.println("Encode name:" + temp);
oBuf.append("UName=" + temp);
………
HttpConnection con = null;
OutputStream os = null;
………
con.setRequestMethod(HttpConnection.POST);
on.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
os = con.openOutputStream();
os.write(oBuf.toString().getBytes());
………
在文本框中输入“白云”发送到服务器,在WTK上输出:
Get textfield name :??
Encode name:/u767d/u4e91
服务器端使用”ISO8859-1”编码格式接收和发送数据。
HttpServletResponse response;
response.setContentType("text/plain; charset=ISO8859-1");
HttpServletRequest request;
request.setCharacterEncoding("ISO8859-1");
服务器接收:
HttpServletRequest request;
request.setCharacterEncoding("ISO8859-1");
nickName = request.getParameter("UName");
System.out.println("Request get:" + nickName);
nickName = decodeUnicode(nickName);
System.out.println("DecodeName :" + nickName);
服务器输出:
Request get:/u767d/u4e91
DecodeName :白云
这时在服务器就可以使用解码后的中文字符串进行处理了,比如存入数据库(要注意数据库的内码格式,如:存入MySQL,字段编码格式可选择UTF-8格式)
//2
一个读取属性文件的类
在编写服务器端代码的时候,常常需要设置一些服务器的属性入Host,Port,databaseUrl等等,往往写在后缀为properties 的属性文件中,下面是一个读取属性文件属性和值的类。
Properties.java
==========================================
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.io.InputStream;
import java.io.BufferedReader;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
/**
*
* 由于java.util.Properties 对应的是ISO 8859-1编码方式。
* 所以其他编码的属性文件需要自行处理。
* 基本功能:
* 以“#”,“!”开头的行被当做注释。
* 属性项与值的格式为:key=value
* 值的前后引号在这里被删除。
*
*/
public class Properties {
public static final Pattern pComment = Pattern.compile("^[#!].*$");
public static final Pattern pKeyVal = Pattern.compile("^([//.//w]+)//s*=//s*(.*)$");
public static final Pattern pQuote = Pattern.compile("^/"(.*)/"$");
public static String encodeType = "ISO 8859-1";
public final HashMap keyval = new HashMap();
public void load(String name) throws IOException {
InputStream inStream = Properties.class.getResourceAsStream(name);
InputStreamReader inReader = new InputStreamReader(inStream, encodeType);
BufferedReader in = new BufferedReader(inReader);
String line = null;
while((line = in.readLine()) != null) {
line = line.trim();
if (line.length() == 0) {
continue;
}
// comment line
Matcher m = pComment.matcher(line);
if (m.matches()) {
continue;
}
// key=value
m = pKeyVal.matcher(line);
if (!m.matches()) {
continue;
}
String key = new String(m.group(1));
String val = new String(m.group(2));
m = pQuote.matcher(val);
if (m.matches()) {
val = new String(m.group(1));
}
keyval.put(key, val);
}
}
public Properties(String fileName,String _encodeType) throws FileNotFoundException, IOException
{
load(fileName);
if( _encodeType != null) {
this.encodeType = _encodeType ;
}
}
public String getStrValue(String key) throws UnsupportedEncodingException {
String val = null;
Object o = keyval.get(key);
if (o instanceof String) {
val = (String)o;
}
return val;
}
public int getIntValue(String key) throws UnsupportedEncodingException {
int val = 0;
try {
Object o = keyval.get(key);
if (o instanceof String) {
val = Integer.parseInt((String)o);
}
}
catch (Exception e) {
}
return val;
}
}
- 手机中文字符网络传输的解决方案//the class of read props
- 手机中文字符网络传输的解决方案
- 有关手机中文问题传输的解决办法
- 有关手机中文问题传输的解决办法
- Uncaught TypeError: Cannot read property ‘props’ of undefined
- gSoap传输中文出现乱码的解决方案
- j2me 的中文网络传输的处理
- QT 网络传输中文
- vue props 拿不到值的解决方案
- Cufon中文字符无法识别的解决方案
- Unity3D脚本中文字符乱码的解决方案
- PHP查找中文字符的解决方案
- Unity3D脚本中文字符乱码的解决方案
- Unity3D脚本中文字符乱码的解决方案
- PHP查找中文字符的解决方案
- 链接中传输中文解决方案
- C#串口传输中文字符
- C#串口传输中文字符
- 三大编程语言的性能PK--Java, C/C++和Ruby (原文最终修订于 2006-09-05 下午06:19:40)
- 十大不良习惯损害大脑
- 深入解析ATL(第二版ATL8.0)(1.4-1.7节)
- 常用的脚本验证
- 感觉LIVE.COM模仿WALLOP.COM
- 手机中文字符网络传输的解决方案//the class of read props
- ADO.NET 中DataTable中加载数据方法
- PHP安全(续1)
- 关于软件和软件工程
- 8月30日 感恩的心真诚的说“谢谢你们”
- url编码的字符对应表
- Windows2000/XP启动过程详解
- ASP.NET2.0 数据绑定函数Eval()的机制
- java的Date类型转换成MySQL的DateTime类型