请问servlet用getInputStream取得流以后,怎么解决中文问题啊?按照书上的写法始终不行!

xlyyc 2005-03-28 05:40:06
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
ServletInputStream in = request.getInputStream();
StringBuffer buffer = new StringBuffer();
BufferedInputStream bis = new BufferedInputStream(in);
int c = -1;
while ( (c = bis.read()) > -1) {
buffer.append( (char) c);
}
String text = buffer.toString();
String returntext = transCode(text, "GBK");//就这样转化中文的啊,可是不行!!
PrintWriter out = response.getWriter();
out.println(returntext);
bis.close();
}

protected String transCode(String value, String enc) {
try {
if (value == null) {
return null;
} else {
value = value.trim();
value = new String(value.getBytes("ISO8859-1"), enc);
return value;
}
} catch (Exception e) {
return null;
}
}
...全文
302 点赞 收藏 14
写回复
14 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
xlyyc 2005-03-30
哈哈,找到原因了,我用IE的表单提交的,在SERLVLET中要转换一下URL的编码,哈哈,谢谢楼上大家关注!!
回复
nwsl 2005-03-29
value = new String(value.getBytes("iso8859_1"), enc);
或者
value = new String(value.getBytes(enc,"iso8859_1");

回复
xlyyc 2005-03-29
up
回复
xlyyc 2005-03-29
谢谢darkattack(居士),都试过啦,不行。下面是代码:

public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html; charset=GBK");
ServletInputStream in = request.getInputStream();
StringBuffer buffer = new StringBuffer();
int c = -1;
while ( (c = in.read()) > -1) {
buffer.append( (char) c);
}
String text = buffer.toString();
String returntext = getISOCode(text);
System.out.println(returntext);
PrintWriter out = response.getWriter();
out.println(returntext);
}

public String getISOCode(String chineseString) {
if (chineseString == null) {
return "";
}
if (chineseString.length() == 0) {
return "";
}
try {
chineseString = new String(chineseString.getBytes(), "ISO-8859-1");
} catch (Exception ex) {
ex.printStackTrace();
}
return chineseString;
}
回复
darkattack 2005-03-29
还有啊,"8859-1"换成"8859_1"
回复
darkattack 2005-03-29
chineseString = new String(chineseString.getBytes(), "8859-1");
或者
chineseString = new String(chineseString.getBytes("GBK"), "8859-1");
或者
chineseString = new String(chineseString.getBytes("UTF-8"), "8859-1");

反正瞎试呗。
回复
xlyyc 2005-03-29
自己UP
回复
darkattack 2005-03-29
"ISO-8859-1"不要"ISO-"试过了?
8859-1 中划线改下划线,都试过了啊?
回复
xlyyc 2005-03-29
up
回复
xlyyc 2005-03-29
天啊,这个问题有这么困难吗?搞了一天多了!!!!!

谢谢,阿甘,但是问题还没有解决!
回复
xlyyc 2005-03-28
UP
回复
xlyyc 2005-03-28
谢谢楼上关注,但是按照你的代码测试还是不行。
回复
jianghuxing 2005-03-28
public String getISOCode(String chineseString) {
if (chineseString == null)
return "";
if (chineseString.equals("null"))
return "";
if (chineseString.length() == 0)
return "";
try {
chineseString = new String(chineseString.getBytes(), "ISO-8859-1");
} catch (Exception ex) {
ex.printStackTrace();
}
return chineseString;
}
回复
xlyyc 2005-03-28
up
回复
相关推荐
发帖
Java EE
创建于2007-09-28

6.6w+

社区成员

J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
申请成为版主
帖子事件
创建了帖子
2005-03-28 05:40
社区公告
暂无公告