KuaiDi100.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package com.iamberry.rst.utils;
  2. import net.sf.json.JSONObject;
  3. import org.springframework.stereotype.Component;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.net.URLConnection;
  9. /**
  10. * Created by Administrator on 2017/8/15.
  11. */
  12. @Component
  13. public class KuaiDi100 {
  14. private static String key = "29833628d495d7a5";//授权密匙key 百度的
  15. /*private static String key = "06d6c2ea1bdb4ee9";//授权密匙key 自己的*/
  16. /*<option value="sto">申通快递</option>
  17. <option value="yto">圆通快递</option>
  18. <option value="sf">顺丰快递</option>
  19. <option value="ems">邮政EMS</option>
  20. <option value="zto">中通快递</option>
  21. <option value="zjs">宅急送</option>
  22. <option value="yunda">韵达快递</option>
  23. <option value="cces">cces快递</option>
  24. <option value="pick">上门提货</option>
  25. <option value="htky">汇通快递</option>
  26. <option value="ttkdex">天天快递</option>
  27. <option value="stars">星晨急便</option>
  28. <option value="jd">京东快递</option>
  29. <option value="01">其他</option>
  30. <option value="02">上门送货</option>*/
  31. public String logisticsConversions(String code){
  32. if(code == null || code.equals("")){
  33. return null;
  34. }
  35. switch (code){
  36. case "sto":
  37. code = "shentong";
  38. break;
  39. case "yto":
  40. code = "yuantong";
  41. break;
  42. case "sf":
  43. code = "shunfeng";
  44. break;
  45. case "ems":
  46. code = "ems";
  47. break;
  48. case "eyb":
  49. code = "ems";
  50. break;
  51. case "zto":
  52. code = "zhongtong";
  53. break;
  54. case "zjs":
  55. code = "zhaijisong";
  56. break;
  57. case "yunda":
  58. code = "yunda";
  59. break;
  60. case "cces":
  61. code = "cces";
  62. break;
  63. case "pick":
  64. code = "pick";
  65. break;
  66. case "htky":
  67. code = "huitongkuaidi";
  68. break;
  69. case "ttkdex":
  70. code = "tiantian";
  71. break;
  72. case "stars":
  73. code = "xingchenjibian";
  74. break;
  75. case "jd":
  76. code = "jd";
  77. break;
  78. case "01":
  79. code = "01";
  80. break;
  81. case "02":
  82. code = "01";
  83. break;
  84. case "dbkd":
  85. code = "debangwuliu";
  86. break;
  87. case "lianhaowuliu":
  88. code = "lianhaowuliu";
  89. break;
  90. }
  91. return code;
  92. }
  93. /**
  94. * 快递对应代码
  95. * EMS ems/eyb
  96. * 申通快递 shentong
  97. * 顺丰速运 shunfeng
  98. * 圆通速递 yuantong
  99. * 韵达快运 yunda
  100. * 中通快递 zhongtong
  101. * 百世快递 huitongkuaidi
  102. * 天天快递 tiantian
  103. * 宅急送 zhaijisong
  104. * 邮政国内包裹 youzhengguonei
  105. * 邮政国际包裹 youzhengguoji
  106. * EMS国际快递 emsguoji
  107. */
  108. public static void main(String[] agrs){
  109. KuaiDi100 kuaidi = new KuaiDi100();
  110. System.out.println(kuaidi.getExpressInfo("ems","9754210807242"));
  111. }
  112. /**
  113. * 查询快递信息
  114. * @param com 快递公司代码
  115. * @param nu 快递单号
  116. * @return
  117. */
  118. public JSONObject getExpressInfo(String com , String nu){
  119. JSONObject ret = null;
  120. try
  121. {
  122. com = logisticsConversions(com);
  123. StringBuilder sb = new StringBuilder();
  124. sb.append("http://api.kuaidi100.com/api?id=");
  125. sb.append(KuaiDi100.key);
  126. sb.append("&com=").append(com);
  127. sb.append("&nu=").append(nu);
  128. sb.append("&show=0&muti=1&order=desc");
  129. URL url= new URL(sb.toString());
  130. URLConnection con=url.openConnection();
  131. con.setAllowUserInteraction(false);
  132. InputStream urlStream = url.openStream();
  133. String type = con.guessContentTypeFromStream(urlStream);
  134. String charSet=null;
  135. if (type == null)
  136. type = con.getContentType();
  137. if (type == null || type.trim().length() == 0)
  138. return null;
  139. if(type.indexOf("charset=") > 0)
  140. charSet = type.substring(type.indexOf("charset=") + 8);
  141. byte b[] = new byte[10000];
  142. int numRead = urlStream.read(b);
  143. String content = new String(b, 0, numRead, "UTF-8");
  144. while (numRead != -1) {
  145. numRead = urlStream.read(b);
  146. if (numRead != -1) {
  147. //String newContent = new String(b, 0, numRead);
  148. String newContent = new String(b, 0, numRead, "UTF-8");
  149. content += newContent;
  150. }
  151. }
  152. content = content.replaceAll(":null", ":\"\"");
  153. ret = JSONObject.fromObject(content);
  154. System.out.println(ret);
  155. urlStream.close();
  156. } catch (MalformedURLException e)
  157. {
  158. e.printStackTrace();
  159. } catch (IOException e)
  160. {
  161. e.printStackTrace();
  162. }
  163. return ret;
  164. }
  165. /**
  166. * 该接口使用与收费的快递公司接口查询
  167. * @param com 快递公司代码
  168. * @param nu 快递单号
  169. * @return
  170. */
  171. public static String searchkuaiDiInfo(String com, String nu)
  172. {
  173. String content = "";
  174. try
  175. {
  176. StringBuilder sb = new StringBuilder();
  177. sb.append("http://www.kuaidi100.com/applyurl?key=").append(KuaiDi100.key);
  178. sb.append("&com=").append(com);
  179. sb.append("&nu=").append(nu);
  180. URL url = new URL(sb.toString());
  181. URLConnection con = url.openConnection();
  182. con.setAllowUserInteraction(false);
  183. InputStream urlStream = url.openStream();
  184. byte b[] = new byte[10000];
  185. int numRead = urlStream.read(b);
  186. content = new String(b, 0, numRead);
  187. while (numRead != -1)
  188. {
  189. numRead = urlStream.read(b);
  190. if (numRead != -1)
  191. {
  192. // String newContent = new String(b, 0, numRead);
  193. String newContent = new String(b, 0, numRead, "UTF-8");
  194. content += newContent;
  195. }
  196. }
  197. urlStream.close();
  198. }
  199. catch (Exception e)
  200. {
  201. e.printStackTrace();
  202. System.out.println("快递查询错误");
  203. }
  204. return content;
  205. }
  206. /**物流名称替换**/
  207. public static String replace(String code) {
  208. String name = "";
  209. switch (code){
  210. case "sto":
  211. name= "申通快递";
  212. break;
  213. case "yto":
  214. name= "圆通快递";
  215. break;
  216. case "zto":
  217. name= "中通速递";
  218. break;
  219. case "sf":
  220. name= "顺丰快递";
  221. break;
  222. case "ems":
  223. name= "邮政EMS";
  224. break;
  225. case "eyb":
  226. name= "E邮宝";
  227. break;
  228. case "zjs":
  229. name= "宅急送";
  230. break;
  231. case "yunda":
  232. name= "韵达快递";
  233. break;
  234. case "cces":
  235. name= "cces快递";
  236. break;
  237. case "pick":
  238. name= "上门提货";
  239. break;
  240. case "htky":
  241. name= "汇通快递";
  242. break;
  243. case "ttkdex":
  244. name= "天天快递";
  245. break;
  246. case "stars":
  247. name= "星晨急便";
  248. break;
  249. case "jd":
  250. name= "京东快递";
  251. break;
  252. case "01":
  253. name= "其他";
  254. break;
  255. case "02":
  256. name= "上门送货";
  257. break;
  258. case "lbwl":
  259. name= "龙邦快递";
  260. break;
  261. default:
  262. break;
  263. case "dbkd":
  264. name = "德邦快递";
  265. break;
  266. }
  267. return name;
  268. }
  269. /**
  270. * 获取快递公司
  271. * @param firm
  272. * @return
  273. */
  274. public String getPostFirm(String firm) {
  275. String postFirm = "";
  276. switch (firm) {
  277. case "sto":postFirm = "申通快递";break;
  278. case "yto":postFirm = "圆通快递";break;
  279. case "sf":postFirm = "顺丰快递";break;
  280. case "ems":postFirm = "邮政EMS";break;
  281. case "zto":postFirm = "中通快递";break;
  282. case "zjs":postFirm = "宅急送";break;
  283. case "yunda":postFirm = "韵达快递";break;
  284. case "cces":postFirm = "cces快递";break;
  285. case "pick":postFirm = "上门提货";break;
  286. case "htky":postFirm = "汇通快递";break;
  287. case "ttkdex":postFirm = "天天快递";break;
  288. case "stars":postFirm = "星晨急便";break;
  289. case "jd":postFirm = "京东快递";break;
  290. case "01":postFirm = "其他";break;
  291. case "02":postFirm = "上门送货";break;
  292. case "dbkd":postFirm = "德邦快递";break;
  293. default:postFirm = "申通快递";break;
  294. }
  295. return postFirm;
  296. }
  297. }