您的位置:首页 > Web前端 > HTML

utf-8 转为 gb2312 和 smartupload上传文件并附text域的值的汉字解决办法(注意是:WAP,XHTML或XML中)

2006-10-10 11:11 567 查看
1. utf-8 转为 gb2312
<%@page contentType="text/html; charset=gb2312"%><%@page session="true" %>
<html>
<head>
<meta http-equiv="Cache-Control" content="max-age=0" forua="true"/>
<meta http-equiv="Cache-control" content="no-cache" forua="true"/>
</head>
<body>
<p>
<%
String str="你好莱坞";
out.print(enCode(str));
out.print("<br/>"+deCode(enCode(str)));
%>
<br/>
</p>
</body>
</html>

<%!
String enCode(String str)
{
if(str==null)return "";
String hs="";

try
{
byte b[]=str.getBytes("UTF-16");
//System.out.println(byte2hex(b));
for (int n=0;n<b.length;n++)
{
str=(java.lang.Integer.toHexString(b
& 0XFF));
if (str.length()==1)
hs=hs+"0"+str;
else
hs=hs+str;
if (n!=b.length-1)hs=hs+"";
}

str= hs.toUpperCase().substring(4);
//System.out.println(str);
char[] chs=str.toCharArray();
str="";
for(int i=0;i<chs.length;i=i+4)
{
str+="&#x"+chs[i]+chs[i+1]+chs[i+2]+chs[i+3]+";";
}

}
catch(Exception e)
{
System.out.print(e.getMessage());
}
return str;
}

String deCode(String str)
{
if (str == null)
{
return "";
}
String temp = "";
String return_temp = "";
try
{
int str_len = str.length() / 7;
for (int i = 1; i <= str_len; i++)
{
temp += str.substring(3 * i + 5 * (i - 1), 7 * i + (i - 1));
}
temp = "FEFF" + temp;
temp = temp.toLowerCase();
int temp_len = temp.length() / 2;
byte mm[] = new byte[temp_len];
String ss[] = new String[temp_len];
for (int i = 0; i < temp_len; i++)
{
String qq = temp.substring(i * 2, i * 2 + 1);
if (!qq.equals("0"))
{
ss[i] = temp.substring(i * 2, i * 2 + 2);
}
else
{
ss[i] = temp.substring(i * 2 + 1, i * 2 + 2);
}
mm[i] = (byte) Integer.parseInt(ss[i], 16);
System.out.println(mm[i]);
}
return_temp = new String(mm, "UTF-16");
}
catch (Exception e)
{
System.out.print(e.getMessage());
}
return return_temp;
}

%>
2.smartupload上传文件并附text域的值的汉字解决办法(注意是:WAP,XHTML或XML中,HTML中则不要转换)
<%@ page contentType="application/vnd.wap.xhtml+xml;charset=UTF-8" import="javax.servlet.*,com.framework.*,com.jspsmart.upload.*, java.util.*,java.lang.*"%><?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Wap2.0上传文件并同时发表文章</title>
</head>
<body>
<p>
<%
// 初始化
final int MaxFileSize=1024000*4; //上传文件大小限制
String ErrMsg=null; //错误信息
boolean ERR=false; //错误标志
com.jspsmart.upload.SmartUpload mySmartUpload=new com.jspsmart.upload.SmartUpload();
mySmartUpload.initialize(pageContext);

// 上传文件
mySmartUpload.upload();

String authorName=mySmartUpload.getRequest().getParameter("authorName");
out.println(".authorName="+new String(java.net.URLDecoder.decode(_authorName,"utf-8").getBytes("gbk") ));

............

%>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐