中文乱码原理代码:

 String s = "中文";
byte[] bs2 = s.getBytes("utf-8");//将s拆成:utf-8个体,注:utf-8指s原本就是utf-8
String s1 = new String(bs2,"iso-8859-1");//将bs2组装成:iso-8859-1串
String s2 = new String(bs2,"utf-8");//将bs2组装成:utf-8串
String s3 = new String(s1.getBytes("iso-8859-1"),"utf-8");//将s1拆成iso-8859-1串,然后组装成utf-8
System.out.println(s1+"\n"+s2+"\n"+s3);

结果:

??????
中文
中文
实例源码下载:http://download.csdn.net/detail/poiuy1991719/8594485

解决HttpClient中文乱码,项目演示:

1:编写httpClient_001

所需包:

1、1:编写URLUtil类

package com.west.test.httpclient;

public class URLUtil {
public static final String HttpClient_002="http://localhost:8080/httpClient_002/";//访问本地项目2路径
public static final String POST_CONTENT=HttpClient_002+"PostContent";
}

1、2:编写Servlet类

package com.west.test.httpclient;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import javax.servlet.ServletException; public class PostServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("========httpClient_001 PostServlet start=========");
System.out.println("httpClient_001:doPost方式提交");
HttpClient httpClient = new HttpClient();
PostMethod method = new PostMethod(URLUtil.POST_CONTENT);
// 解决中文乱码(设置:"我方"HttpClient提交、解析所用码)
method.getParams().setContentCharset("utf-8"); String charset=method.getParams().getContentCharset();
NameValuePair name = new NameValuePair("name", "张三");
NameValuePair password = new NameValuePair("password",
"password:123321");
method.setRequestBody(new NameValuePair[] { name, password });
String rt = "";
try {
int status = httpClient.executeMethod(method);
if (status == HttpStatus.SC_OK) {
rt = method.getResponseBodyAsString();
System.out.println("httpClient_001得到:" + rt);
codeTest(rt,charset);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("网络连接失败,请联系管理员!");
}
// 释放HttpClient资源
method.releaseConnection();
outMessage(response, rt); } protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("httpClient_001:doGet方式提交");
doPost(request, response); } public void outMessage(HttpServletResponse response, String message) {
try {
// 解决"界面"中文乱码(设置:"我方"提交所用码、"对方"解析所用码)
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print(message);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
} public void codeTest(String rt ,String charset) throws Exception { System.out.println("得到的是:"+charset);
byte[] btr=rt.getBytes(charset);
String rt01 = new String(btr, "iso-8859-1");
System.out.println("String(btr,'iso-8859-1')转码01:" + rt01);
String rt02 = new String(btr, "utf-8");
System.out.println("String(btr,'utf-8')转码02:" + rt02);
String rt03 = new String(rt01.getBytes("iso-8859-1"), "utf-8");
System.out.println(rt03); }
}

1.3:配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>test</display-name> <servlet>
<servlet-name>PostServlet</servlet-name>
<servlet-class>com.west.test.httpclient.PostServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PostServlet</servlet-name>
<url-pattern>/PostServlet</url-pattern>
</servlet-mapping> <session-config>
<session-timeout></session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

1、3:编写界面:index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>test</title>
</head>
<body>
<form action="PostServlet" method="post">
<input type="submit" value="Post提交">
</form>
</body>
</html>

2:编写项目:httpClient_002

2、1:编写Servlet类

package com.test.servlet;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class PostContent extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("=========httpClient_002 PostContent start============");
System.out.println("httpClient_002:doPost方式执行");
// 解决中文乱码(设置:"我方"解析所用码)
request.setCharacterEncoding("utf-8"); String name = request.getParameter("name");
String password = request.getParameter("password");
System.out.println("httpClient_002得到:{name:" + name+",password:"+password+"}");
String reStr = "{name=" + name + ",password="
+ password+"}";
outMessage(response, reStr);
} public void outMessage(HttpServletResponse response, Object message) {
try {
// 解决中文乱码(设置:"我方"提交所用码,"httpClient_001"解析所用码)
response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter();
out.print(message);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

2.2:配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>test002</display-name> <servlet>
<servlet-name>PostContent</servlet-name>
<servlet-class>com.test.servlet.PostContent</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PostContent</servlet-name>
<url-pattern>/PostContent</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
</welcome-file-list>
</web-app>

3:总结

1、解决中文乱码(设置:"我方"HttpClient提交、解析所用码)
method.getParams().setContentCharset("utf-8"); 2、解决"界面"中文乱码(设置:"我方"提交所用码、"对方"解析所用码)
response.setContentType("text/html;charset=utf-8"); 3、解决中文乱码(设置:"我方"解析所用码)
request.setCharacterEncoding("utf-8"); 4、解决中文乱码(设置:"我方"提交所用码、"httpClient_001"解析所用码),同2
response.setContentType("text/html;charset=utf-8");

附加:

请求网页、servlet:

GetMethod getMethod = new GetMethod("http://www.baidu.com");
//(1)、这里可以设置自己想要的编码格式
getMethod.getParams().setContentCharset("utf-8"); //(2)、对于get方法也可以这样设置
getMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"GB2312"); //(3)、还可以如下这样设置
getMethod.addRequestHeader("Content-Type", "text/html; charset=UTF-8"); //(4)、当然同样可以直接设置 httpClient 对象的编码格式
HttpClient httpClient = new HttpClient();
httpClient.getParams().setContentCharset("utf-8"); //使用流的方式读取也可以如下设置
InputStream in = getMethod.getResponseBodyAsStream();
//这里的编码规则要与上面的相对应
BufferedReader br = new BufferedReader(new InputStreamReader(in,"utf-8"));

请求方法、servlet:

PostMethod PostMethod= new PostMethod("http://localhost:8080/ezid-cert-mobile/upload");
//(1)、通常可以如下设置自己想要的编码格式
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8"); //(2)、也重载PostMethod的getRequestCharSet()方法
public class UTF8PostMethod extends PostMethod{
public UTF8PostMethod(String url){
super(url);
}
@Override
public String getRequestCharSet() {
return "UTF-8";
}
} //(3)、如果是方法的参数出现乱码问题,那么你可以如下设置参数
Charset utf8Charset = Charset.forName("UTF-8");
multipartContent.addPart("name", new StringBody(Info.getUserEntity().getName(), utf8Charset)); //(4)、如果你用的是Part [] parts={...}传参方式的话可以如下设置
StringPart name=new StringPart("name",certFormEntity.getPersonName(), "UTF-8");

HttpClient_002_中文乱码、HttpClient中文乱码透析、总结的更多相关文章

  1. Firebug中调试中的js脚本中中文内容显示为乱码

    Firebug中调试中的js脚本中中文内容显示为乱码 设置 页面 UFT-8 编码没用, 解决方法:点击 "Firebug"工具栏 中的"选项"---" ...

  2. wkhtmltopdf中文显示空白或者乱码方框

    中文乱码或者空白解决方法 如果wkhtmltopdf中文显示空白或者乱码方框 打开windows c:\Windows\fonts\simsun.ttc拷贝到linux服务器/usr/share/fo ...

  3. ASP.NET开发在JavaScript有中文汉字时出现乱码时简单有效的解决

    一般情况在使用ASP.NET开发使用JavaScript有中文汉字时不会出现乱码情况,比如:alert('您看到我了吗?');这样直接输入中文汉字的代码中是不会出现乱码的,如果出现了,一是检查Web. ...

  4. .net文件压缩和解压及中文文件夹名称乱码问题

    /**************************注释区域内为引用http://www.cnblogs.com/zhaozhan/archive/2012/05/28/2520701.html的博 ...

  5. windows mysql 中文乱码和中文录入提示太大错误的解决方法

    今天操作mysql的时候很郁闷,因为修改默认字符集搞了半天,终于弄成了(关于如何把windows的默认字符集设置成功,可以参看另一篇博文,最终在mysql中输入show variables like ...

  6. php生成的中文文件名会变成乱码,应该这样解决

    现在php有很多类库,会生成文件,比如生成zip文件,生成二维码等等.这些类库用起来很爽,但是一旦生成带有中文的文件名,极有可能出现乱码. 问题:生成的中文文件名会变成乱码 解决:使用函数:iconv ...

  7. DWZ 框架remote 验证字段唯一性方法提交后台,如果是中文会显示成乱码问题

    关于jquery  remote 验证字段唯一性方法提交后台,如果是中文会显示成乱码问题.可以直接修改tomcat 配置文件server.xml  设置 URIEncoding=utf-8属性,将ge ...

  8. Cocos2d—X游戏开发之VS2010 控制台输出中文,模拟器中文乱码问题解决

    首先,先解决第一个问题,我们使用VS2010开发的时候,调试的时候,中文打印出来都是乱码,这个问题很纠结. 如下图: CCLOG("cclog: 测试使用标签的自动换行和个别字体大写&quo ...

  9. 【原创】python中文编码问题深入分析(二):print打印中文异常及显示乱码问题分析与解决

    在学习python以及在使用python进行项目开发的过程中,经常会使用print语句打印一些调试信息,这些调试信息中往往会包含中文,如果你使用python版本是python2.7,或许你也会遇到和我 ...

随机推荐

  1. SDCycleScrollView 滚动视图的使用(广告)

    github库链接https://github.com/gsdios/SDCycleScrollView 无限循环自动图片轮播器(一步设置即可使用) // 网络加载图片的轮播器 cycleScroll ...

  2. FPGA的典型应用领域

    本文关键字:fpga应用,fpga应用领域, fpga培训,FPGA应用开发入门与典型实例 一.数据采集和接口逻辑领域 1.FPGA在数据采集领域的应用 由于自然界的信号大部分是模拟信号,因此一般的信 ...

  3. php xls 导出乱码解决方案

    采用phpmyadmin的处理方式 //这个很关键 BU 订单号 产品ID 产品名称 原价 售价 房间 成人数 小孩数 总人数 出团日期 出团天数差 客人信息 姓名 性别 年龄 总价 下单日期 订单状 ...

  4. Apache Jmeter发送post请求

    下面用Jmeter发送一个post请求, 对应的js代码如下: $("#register_a").click(function() { var name = $("#un ...

  5. vsphere平台windows虚拟机克隆的小插曲

    问题: 1.克隆完windows虚拟化后输入法乱码. 2.开启远程的情况下远程登录输入正确的密码也无法登录. 解决: 1.更改管理员用户密码(不输入原win7密码更改win7密码). 2.重新启用管理 ...

  6. respond.js

    Respond.js,低版本浏览器也能够支持媒体查询 在之前有篇文章也是介绍IE6,7,8支持媒体查询的(查看),Respond.js这个比css3-mediaqueries更为强大一些,它可以支持l ...

  7. sql*loader的直接加载方式和传统加载方式的性能差异

    1.确认数据库版本 2.数据准备 3.创建导入表及控制文件 4.直接加载方式演示 查看具体的日志: 5.传统加载方式演示 查看日志文件: 6.结论及两种方式的差异 经过比对direct比convent ...

  8. [silverlight—wcf]参数:调试资源字符串不可用,秘钥和参数通常提供足够的信息用以诊断问题。

    这段时间在做一个项目,有一项需求是上传,经过思考之后,决定采取Silverlight+WCF的方式做上传操作.就在项目做完了之后,本地测试也都没问题,发布到服务器上的时候,顿时就出现故障了.在选择文件 ...

  9. Android课程---优化ListView列表视图

    activity_ui4.xml <?xml version="1.0" encoding="utf-8"?> <ListView xmlns ...

  10. 在 NetBeans IDE 6.0 中分析 Java 应用程序性能

    NetBeans IDE 6.0 包含一个强大的性能分析工具,可提供与应用程序运行时行为有关的重要信息.通过 NetBeans 性能分析工具,我们可以方便地在 IDE 中监控应用程序的线程状态.CPU ...