社区
Web 开发
帖子详情
用java,怎样把GB2312的中文字转成utf-8编码.
nternter
2005-10-26 10:16:35
用java,怎样把GB2312的中文字转成utf-8编码.
...全文
3020
5
打赏
收藏
用java,怎样把GB2312的中文字转成utf-8编码.
用java,怎样把GB2312的中文字转成utf-8编码.
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
5 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
Mark_MaoHUA
2005-10-26
打赏
举报
回复
String result = new String(tempStr.getBytes("GB2312"),"UTF-8"
zzzz1324
2005-10-26
打赏
举报
回复
我用JSPSMARTUPLOAD控件下载的时候出现此错误:
/jspsmartupload/jsp/12345.jsp:14: method 中的参数数量错误。
b = Character.toString(c).getBytes("utf-8");
我的代码是这样的兄弟看看:
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.io.*"%>
<%!
public String toUtf8String(String s) {///源于网上
StringBuffer sb = new StringBuffer();
for (int i=0;i<s.length();i++) {
char c = s.charAt(i);
if (c >= 0 && c <= 255) {
sb.append(c);
} else {
byte[] b;
try {
b = Character.toString(c).getBytes("utf-8");
String tempStr = "中文";//准备转换的字符
String result = new String(tempStr.getBytes("GB2312"),"UTF-8");//转换后的结果
} catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0) k += 256;
sb.append("%" + Integer.toHexString(k).
toUpperCase());
}
}
}
return sb.toString();
}
%>
<%
String filename="INDEX2.jsp";
String dirName=application.getRealPath("/upload");
java.io.File ff=null;
String dd=dirName+System.getProperties().getProperty("file.separator")+filename;
try{
ff=new java.io.File(dd);
}
catch(Exception e){
e.printStackTrace();
}
if (ff!=null&&ff.exists()&&ff.isFile())
{
long filelength = ff.length();
InputStream inStream=new FileInputStream(dd);
//设置输出的格式
response.reset();
response.setContentType("application/x-msdownload");
response.setContentLength((int)filelength);
response.addHeader("Content-Disposition","attachment; filename=\"" + toUtf8String(filename) + "\"");
//循环取出流中的数据
byte[] b = new byte[100];
int len;
while((len=inStream.read(b)) >0)
response.getOutputStream().write(b,0,len);
inStream.close();
}
%>
洪泉
2005-10-26
打赏
举报
回复
沙发上的正解我也是这样做的!
hzain
2005-10-26
打赏
举报
回复
最好用一个过滤器来做,此类文章可以在网上查一下
zouyu215
2005-10-26
打赏
举报
回复
String tempStr = "中文";//准备转换的字符
String result = new String(tempStr.getBytes("GB2312"),"UTF-8");//转换后的结果
字符转码,
gb2312
usc2 ,
utf-8
字符转码,
gb2312
usc2 ,
utf-8
随意转换
关于
JAVA
字符
编码
:Unicode,ISO-8859-1,GBK,
UTF-8
编码
及相互转换
关于
JAVA
字符
编码
:Unicode,ISO-8859-1,GBK,
UTF-8
编码
及相互转换
JAVA
字符
编码
:Unicode,ISO-8859-1,GBK,
UTF-8
编码
及相互转换
JAVA
字符
编码
:Unicode,ISO-8859-1,GBK,
UTF-8
编码
及相互转换
GBK
GB2312
UTF-8
ISO-8859-1区别
UTF-8
:Unicode Transformation Format-8bit,许多的公司联合决定制定一个包含全世界所有
文字
的巨大字集,其
中
也有亚洲的公司参与。
GB2312
UTF-8
字符互转
可以把
UTF-8
%%%形式的字符
转成
中
文,把
中
文
转成
UTF-8
%%%形式
Web 开发
81,116
社区成员
341,738
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章