通过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:????
有谁知道是哪里的问题吗?
这样试试:
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的项目属性也设置了
不知道是什么原因。搞了两天了,要疯了。
![](https://image.cha138.com/20230507/41ba3f1fbe2843c081649c4e7cbde210.jpg)
为什么会乱?能说出原因吗?我就是要接收中文的信息啊
怎么用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包
纯手工打字,请采纳哈
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页面,提交数据到服务器?