通过html页面的表单提交中文数据,Java后台出现乱码。跪求大神解答。
Java后台的代码是:StringstrChineseString=request.getParameter("addr");Stringencoding=System....
Java后台的代码是:
String strChineseString = request.getParameter("addr");
String encoding = System.getProperty("file.encoding");
System.out.println("系统默认的字符集是:" + encoding);
System.out .println("Charset.forName(\"GBK\")).length:"+strChineseString.getBytes(Charset.forName("GBK")).length);
System.out .println("Charset.forName(\"UTF-8\")).length:"+strChineseString.getBytes(Charset.forName("UTF-8")).length);
System.out.println("strChineseString.getBytes().length:"+strChineseString.getBytes().length);
byte[] bytesgbk=strChineseString.getBytes("gbk");
System.out.println("gbk->utf8:"+new String (bytesgbk,"utf-8")); byte[] bytesiso=strChineseString.getBytes("iso-8859-1");
System.out.println("iso->utf8:"+new String (bytesiso,"utf-8"));
byte[] bytesgb2312=strChineseString.getBytes("gb2312");
System.out.println("gb2312->utf8:"+new String (bytesgb2312,"utf-8"));
通过浏览器提交表单后,打印下面的结果:
系统默认的字符集是:GBK Charset.forName("GBK")).length:4
Charset.forName("UTF-8")).length:12
strChineseString.getBytes().length:4
gbk->utf8:????
iso->utf8:????
gb2312->utf8:????
有谁知道是哪里的问题吗? 展开
String strChineseString = request.getParameter("addr");
String encoding = System.getProperty("file.encoding");
System.out.println("系统默认的字符集是:" + encoding);
System.out .println("Charset.forName(\"GBK\")).length:"+strChineseString.getBytes(Charset.forName("GBK")).length);
System.out .println("Charset.forName(\"UTF-8\")).length:"+strChineseString.getBytes(Charset.forName("UTF-8")).length);
System.out.println("strChineseString.getBytes().length:"+strChineseString.getBytes().length);
byte[] bytesgbk=strChineseString.getBytes("gbk");
System.out.println("gbk->utf8:"+new String (bytesgbk,"utf-8")); byte[] bytesiso=strChineseString.getBytes("iso-8859-1");
System.out.println("iso->utf8:"+new String (bytesiso,"utf-8"));
byte[] bytesgb2312=strChineseString.getBytes("gb2312");
System.out.println("gb2312->utf8:"+new String (bytesgb2312,"utf-8"));
通过浏览器提交表单后,打印下面的结果:
系统默认的字符集是:GBK Charset.forName("GBK")).length:4
Charset.forName("UTF-8")).length:12
strChineseString.getBytes().length:4
gbk->utf8:????
iso->utf8:????
gb2312->utf8:????
有谁知道是哪里的问题吗? 展开
3个回答
展开全部
既然项目设置的是utf-8,编码就全部设置为utf-8啊,jsp页面的也要设置为utf-8编码。
这样试试:
equest.setCharacterEncoding("utf-8");
String strChineseString = request.getParameter("addr");
System.out.println("strChineseString :"+strChineseString );
strChineseString =new String(strChineseString.getBytes("iso-8859-1"),"utf-8");
System.out.println("strChineseString :"+strChineseString );
这样试试:
equest.setCharacterEncoding("utf-8");
String strChineseString = request.getParameter("addr");
System.out.println("strChineseString :"+strChineseString );
strChineseString =new String(strChineseString.getBytes("iso-8859-1"),"utf-8");
System.out.println("strChineseString :"+strChineseString );
展开全部
form表单里加个 method="post"
更多追问追答
追问
就是通过post提交的
追答
response.setContentType("text/html");
response.setCharacterEncoding("utf-8"); 后台开始加这句话试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你这个不乱码都没道理啊,不知道你要干什么?
追问
为什么会乱?能说出原因吗?我就是要接收中文的信息啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询