请问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;
}
}
...全文
334 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
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

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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