Jsp使用阿里云全球物流快递查询(单号识别)乱码问题
使用阿里云全球物流快递查询(单号识别)的时候,通过流读取数据时,遇到了乱码问题,在.java文件中直接运行可以正常显示,但是使用jsp调用.java时却遇到了乱码问题,官方的示例是下面这样的
public static void main(String[] args) {
String host = "https://goexpress.market.alicloudapi.com";// 【1】请求地址 支持http 和 https 及 WEBSOCKET
String path = "/goexpress";// 【2】后缀
String appcode = "你自己的AppCode"; // 【3】开通服务后 买家中心-查看AppCode
String no = "780098068058"; // 【4】请求参数,详见文档描述
String type = "zto"; // 【4】请求参数,不知道可不填 95%能自动识别
String urlSend = host + path + "?no=" + no + "&type=" + type; // 【5】拼接请求链接
try {
URL url = new URL(urlSend);
HttpURLConnection httpURLCon = (HttpURLConnection) url.openConnection();
httpURLCon.setRequestProperty("Authorization", "APPCODE " + appcode);// 格式Authorization:APPCODE
// (中间是英文空格)
int httpCode = httpURLCon.getResponseCode();
if (httpCode == 200) {
String json = read(httpURLCon.getInputStream());
System.out.println("正常请求计费(其他均不计费)");
System.out.println("获取返回的json:");
System.out.print(json);
} else {
Map<String, List<String>> map = httpURLCon.getHeaderFields();
String error = map.get("X-Ca-Error-Message").get(0);
if (httpCode == 400 && error.equals("Invalid AppCode `not exists`")) {
System.out.println("AppCode错误 ");
} else if (httpCode == 400 && error.equals("Invalid Url")) {
System.out.println("请求的 Method、Path 或者环境错误");
} else if (httpCode == 400 && error.equals("Invalid Param Location")) {
System.out.println("参数错误");
} else if (httpCode == 403 && error.equals("Unauthorized")) {
System.out.println("服务未被授权(或URL和Path不正确)");
} else if (httpCode == 403 && error.equals("Quota Exhausted")) {
System.out.println("套餐包次数用完 ");
} else {
System.out.println("参数名错误 或 其他错误");
System.out.println(error);
}
}
} catch (MalformedURLException e) {
System.out.println("URL格式错误");
} catch (UnknownHostException e) {
System.out.println("URL地址错误");
} catch (Exception e) {
// 打开注释查看详细报错异常信息
// e.printStackTrace();
}
}
/*
* 读取返回结果
*/
private static String read(InputStream is) throws IOException {
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = br.readLine()) != null) {
line = new String(line.getBytes(), "utf-8");
sb.append(line);
}
br.close();
return sb.toString();
}
本来以为是jsp或者java文件编码的问题,后来打断点发现httpURLCon.getInputStream()获取的流是gbk编码,修改一下编码即可
/*
* 读取返回结果
*/
private static String read(InputStream is) throws IOException {
StringBuffer sb = new StringBuffer();
//new 流的时候添加编码即可
BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
line = new String(line.getBytes());
sb.append(line);
}
br.close();
return sb.toString();
}