直接讲Parameter参数放在url里面默认使用的是iso8859-1编码,而且connection.setRequestProperty设置的编码不针对url只设置输出流的data,所以此时乱码的解决方案:
1、webx端重新编码,String txt = new String(desc.toString().getBytes("iso8859-1"), "utf-8");
2、放在data中,输出流write出去,
outStream.write(data.getBytes());
outStream.flush();
上面代码会产生另外一个问题:
getBytes()默认使用文件编码
public static Charset defaultCharset() {
if (defaultCharset == null) {
synchronized (Charset.class) {
String csn = AccessController.doPrivileged(
new GetPropertyAction("file.encoding"));
Charset cs = lookup(csn);
if (cs != null)
defaultCharset = cs;
else
defaultCharset = forName("UTF-8");
}
}
return defaultCharset;
}
所以eclipse和idea中结果不同,idea编码正常,因为idea默认就是UTF-8,eclipse乱码,eclipse是GBK,正确的方式是指定一下编码:
outStream.write(data.getBytes("UTF-8"));
outStream.flush();