服务器A 向 服务器B 传输数据,
服务器B接到数据:
两端都在方法里设置了:
request.setCharacterEncoding("UTF-8");
response.setContentType("text/plain; charset=UTF-8");
//开始获取:
String data = request.getParameter("json");
Log.info("发来数据:"+data);
String coding = getEncoding(data);
public static String getEncoding(String str) {
Log.info("--GB2312 :" + new String(str.getBytes(), "GB2312"));
Log.info("--ISO-8859-1 :" + new String(str.getBytes(), "ISO-8859-1"));
Log.info("--UTF-8 :" + new String(str.getBytes(), "UTF-8"));
Log.info("--GBK :" + new String(str.getBytes(), "GBK"));
Log.info("--GB2312 ---2:" + new String(str.getBytes("GB2312")));
Log.info("--ISO-8859-1 ---2:" + new String(str.getBytes("ISO-8859-1")));
Log.info("--UTF-8--2:" + new String(str.getBytes("UTF-8")));
Log.info("--GBK--2 :" + new String(str.getBytes("GBK")));
Log.info("UTF-8--GB2312 :" + new String(str.getBytes("UTF-8"), "GB2312"));
Log.info("UTF-8--ISO-8859-1 :" + new String(str.getBytes("UTF-8"), "ISO-8859-1"));
Log.info("UTF-8--UTF-8 :" + new String(str.getBytes("UTF-8"), "UTF-8"));
Log.info("UTF-8--GBK :" + new String(str.getBytes("UTF-8"), "GBK"));
Log.info("GB2312--UTF-8 :" + new String(str.getBytes("GB2312"), "UTF-8"));
Log.info("GB2312--ISO-8859-1 :" + new String(str.getBytes("GB2312"), "ISO-8859-1"));
Log.info("GB2312--GBK :" + new String(str.getBytes("GB2312"), "GBK"));
Log.info("GB2312--GB2312 :" + new String(str.getBytes("GB2312"), "GB2312"));
Log.info("ISO-8859-1--GBK :" + new String(str.getBytes("ISO-8859-1"), "GBK"));
Log.info("ISO-8859-1--UTF-8 :" + new String(str.getBytes("ISO-8859-1"), "UTF-8"));
Log.info("ISO-8859-1--GB2312 :" + new String(str.getBytes("ISO-8859-1"), "GB2312"));
Log.info("ISO-8859-1--ISO-8859-1 :" + new String(str.getBytes("ISO-8859-1"), "ISO-8859-1"));
Log.info("GBK--GB2312:" + new String(str.getBytes("GBK"), "GB2312"));
Log.info("GBK--ISO-8859-1:" + new String(str.getBytes("GBK"), "ISO-8859-1"));
Log.info("GBK--UTF-8:" + new String(str.getBytes("GBK"), "UTF-8"));
Log.info("GBK--GBK:" + new String(str.getBytes("GBK"), "GBK"));
}
日志截图里 xm:“???” 是中文名字,
无论怎么转换字符集,都是 ??? 格式乱码!
求大神帮忙看看!!!!
内容呢?你发的什么内容?从文件里取的?还是什么?
服务器A 向 服务器B 传输数据, 用的 httpClient (下面getData()方法),
格式是 json = "******" ;
服务器B 获取数据: String data = request.getParameter("json"); 得到的是乱码
public static JSONObject getData(String url,String data) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
try {
httpPost.setEntity(new StringEntity(data));
HttpResponse response = httpClient.execute(httpPost);
JSONObject jsonObject = JSONObject.parseObject(EntityUtils
.toString(response.getEntity()));
return jsonObject;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
用wireshark抓抓包,看看你发了啥吧
先去查发过去的数据是否正常,是否是乱码,再去查接收到的数据哪个环节出现的乱码。
服务器A 发送的时候有日志的,不乱码, 服务器B 接到就乱码,数据根本没做其他处理
问题解决。
忘记了最根本的 encode 问题,
服务器A 发送数据时 String da = URLEncoder.encode(str,"UTF-8");
服务器B 接到数据时 String str = URLDecoder.decode(da,"UTF-8");