通过html页面的表单提交中文数据,Java后台出现乱码。跪求大神解答。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过html页面的表单提交中文数据,Java后台出现乱码。跪求大神解答。相关的知识,希望对你有一定的参考价值。

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:????
有谁知道是哪里的问题吗?

既然项目设置的是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 );
参考技术A form表单里加个 method="post"追问

就是通过post提交的

追答

response.setContentType("text/html");
response.setCharacterEncoding("utf-8"); 后台开始加这句话试试

追问

我只是从request拿数据,与response没关系吧?

追答

request.setCharacterEncoding("gbk");
response.setContentType("text/html;charset=gbk");

String strChineseString = request.getParameter("addr");

System.out.println(strChineseString);

追问

试了,还是不行。我的myeclipse的项目属性也设置了

不知道是什么原因。搞了两天了,要疯了。

参考技术B 你这个不乱码都没道理啊,不知道你要干什么?追问

为什么会乱?能说出原因吗?我就是要接收中文的信息啊

怎么用java模拟浏览器提交html页面的表单数据

HttpClient模拟请求如下
HttpClient  httpclient = new DefaultHttpClient();        //打开浏览器
HttpPost    httpPost = new HttpPost("www.xxx.xxx");    //输入网址

List <NameValuePair> nvps = new ArrayList<NameValuePair>();  
nvps.add(new BasicNameValuePair("userName","123"));   
nvps.add(new BasicNameValuePair("password","123"));   //封装表单
httpPost.setEntity(new UrlEncodedFormEntity(nvps,"utf-8")); //将参数传入post方法中 
HttpResponse response = httpclient.execute(httpPost);    //执行post
HttpEntity   entity   = response.getEntity();    //获取响应数据
String result = EntityUtils.toString(entity);    //将响应数据转成字符串
需要导入jar包
纯手工打字,请采纳哈


参考技术A httpclient就行了,给你个取IP的例子好了

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class IPHelper

public String getSourceText(String ip) throws IOException
String text = null;
HttpClient client = new HttpClient();
client.getParams().setContentCharset("GBK");
PostMethod post = new PostMethod("http://www.ip138.com/ips8.asp");
NameValuePair[] data = new NameValuePair("action", "2"),
new NameValuePair("ip", ip) ;
post.setRequestBody(data);
client.executeMethod(post);
text = post.getResponseBodyAsString();
post.releaseConnection();
return text;


public static void main(String[] args) throws IOException
IPHelper h=new IPHelper();
System.out.println(h.getSourceText("192.169.0.1"));



这个是Post的,还有Get的,看你的form是怎么样的了。
参考技术B 使用组件httpclient,网上有下载。

以上是关于通过html页面的表单提交中文数据,Java后台出现乱码。跪求大神解答。的主要内容,如果未能解决你的问题,请参考以下文章

java web 如何防止 用户绕过js验证,直接地址栏提交表单或自己编写html页面,提交数据到服务器?

Web页面向后台提交数据的方式和选择

java form表单提交到另一个jsp页面,但页面不跳转过去,求指点

layui提交弹出页面的form表单

Html中 from表单提交如何提交数组到java后台?

Java数组,如何通过JSP表单将数组传递到后台?