首页 新闻 会员 周边

两台服务器之间数据传输乱码问题

0
悬赏园豆:20 [待解决问题]

服务器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:“???” 是中文名字,
无论怎么转换字符集,都是 ??? 格式乱码!

求大神帮忙看看!!!!

Li&Fan的主页 Li&Fan | 初学一级 | 园豆:191
提问于:2021-03-16 14:58
< >
分享
所有回答(4)
0

内容呢?你发的什么内容?从文件里取的?还是什么?

顾晓北 | 园豆:10844 (专家六级) | 2021-03-16 16:49

服务器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;
}
}

支持(0) 反对(0) Li&Fan | 园豆:191 (初学一级) | 2021-03-16 17:01
0

用wireshark抓抓包,看看你发了啥吧

yytxdy | 园豆:1680 (小虾三级) | 2021-03-16 17:18
0

先去查发过去的数据是否正常,是否是乱码,再去查接收到的数据哪个环节出现的乱码。

ycyzharry | 园豆:25653 (高人七级) | 2021-03-17 00:44

服务器A 发送的时候有日志的,不乱码, 服务器B 接到就乱码,数据根本没做其他处理

支持(0) 反对(0) Li&Fan | 园豆:191 (初学一级) | 2021-03-17 09:42
0

问题解决。
忘记了最根本的 encode 问题,
服务器A 发送数据时 String da = URLEncoder.encode(str,"UTF-8");
服务器B 接到数据时 String str = URLDecoder.decode(da,"UTF-8");

Li&Fan | 园豆:191 (初学一级) | 2021-03-18 11:25
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册