哪位大虾答一下

hua1111 2003-10-30 01:44:46
在JSP中读一个文本文件,里面是中文,但读出来的是乱码;
曾经试过转换字符
String str = new String(str.getBytes("ISO8859_1"),"GBK");
之类的操作,但没有效果。
file = new BufferedReader(new FileReader(recFileName));
String radioORcheckbox=file.readLine();
String str = new String(radioORcheckbox.getBytes("ISO8859_1"),"GBK");
out.println(str);

...全文
16 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
hua1111 2003-10-31
  • 打赏
  • 举报
回复
不行啊
还有其他方法吗?
9731boy 2003-10-31
  • 打赏
  • 举报
回复
为什么在jsp中读?
bean中也会?
不会吧.读出应该不会出错的.是页面解析时出错的(未转换)
Aileenwyh 2003-10-31
  • 打赏
  • 举报
回复
除了在JSP中声明:<%@ page contentType="text/html; charset=GB2312" %>
还必须保证你的servlet的字符编码也是GB2312,在../web.xml里设置。
Apollo47 2003-10-31
  • 打赏
  • 举报
回复
<%@ page contentType="text/html; charset=GB2312" %>
在Jsp前面加上字符集编码声明。
hua1111 2003-10-31
  • 打赏
  • 举报
回复
救命啊
hua1111 2003-10-31
  • 打赏
  • 举报
回复
不行啊
这个应该与服务器的设置JSP编码有关
尝试打出服务器当前的属性
其中
sun.io.unicode.encoding:UnicodeBig
是不是应该将page 里的charset设为utf-8
但是这样报错。
zwxu 2003-10-31
  • 打赏
  • 举报
回复
进行行读时用gbk就行了
B2China 2003-10-30
  • 打赏
  • 举报
回复
这样处理:
加这么一个处理函数:


<%! //处理中文字符串的方法
public String getString(String s)
{
if(s==null)
{s="";}
try
{
byte b[]=s.getBytes("ISO-8859-1");
s=new String(b);
}
catch(Exception e)
{}
return s;
}

调用:
<% String customer=(String)session.getAttribute("customer");
customer=getString(customer);
%>
zkjbeyond 2003-10-30
  • 打赏
  • 举报
回复
lynx1111 2003-10-30
  • 打赏
  • 举报
回复
楼上幽默
superszhu 2003-10-30
  • 打赏
  • 举报
回复
总有一款适合你

String aa = file.readLine();
System.out.println("---1---" + aa);
System.out.println("---2---" + new String(aa.getBytes(),"UTF-8"));
System.out.println("---3---" + new String(aa.getBytes(),"iso8859-1"));
System.out.println("---4---" + new String(aa.getBytes(),"gb2312"));

System.out.println("===5===" + new String(aa.getBytes("gb2312"), "utf-8"));
System.out.println("===6===" + new String(aa.getBytes("gb2312"), "iso8859-1"));

System.out.println("===7===" + new String(aa.getBytes("utf-8"), "gb2312"));
System.out.println("===8===" + new String(aa.getBytes("utf-8"), "iso8859-1"));

System.out.println("===9===" + new String(aa.getBytes("iso8859-1"), "gb2312"));
System.out.println("===10==" + new String(aa.getBytes("iso8859-1"), "utf-8"));
hua1111 2003-10-30
  • 打赏
  • 举报
回复
gb2312也试过了
或者
String str = new String(radioORcheckbox.getBytes("GBK"),"ISO8859_1");
也试过了。
我说的是读文件
写文件时转换字符后,中文没有乱码;
只是在读文件时,中文出现乱码。
upcmill 2003-10-30
  • 打赏
  • 举报
回复
//用来对写文件的时候重编码
public static String setFileEncoding(String data1)
throws Exception
{

String charset = "GB2312";
String newset = "ISO8859_1";

byte[] str = data1.getBytes(charset);

String data = new String(str,newset);

return data;
}

用这个函数,肯定没错。给分,呵呵!!
lynx1111 2003-10-30
  • 打赏
  • 举报
回复
gb2312呢?

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧