KuaiDi100.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. }
  85. return code;
  86. }
  87. /**
  88. * 快递对应代码
  89. * EMS ems/eyb
  90. * 申通快递 shentong
  91. * 顺丰速运 shunfeng
  92. * 圆通速递 yuantong
  93. * 韵达快运 yunda
  94. * 中通快递 zhongtong
  95. * 百世快递 huitongkuaidi
  96. * 天天快递 tiantian
  97. * 宅急送 zhaijisong
  98. * 邮政国内包裹 youzhengguonei
  99. * 邮政国际包裹 youzhengguoji
  100. * EMS国际快递 emsguoji
  101. */
  102. public static void main(String[] agrs){
  103. KuaiDi100 kuaidi = new KuaiDi100();
  104. System.out.println(kuaidi.getExpressInfo("ems","9754210807242"));
  105. }
  106. /**
  107. * 查询快递信息
  108. * @param com 快递公司代码
  109. * @param nu 快递单号
  110. * @return
  111. */
  112. public JSONObject getExpressInfo(String com , String nu){
  113. JSONObject ret = null;
  114. try
  115. {
  116. com = logisticsConversions(com);
  117. StringBuilder sb = new StringBuilder();
  118. sb.append("http://api.kuaidi100.com/api?id=");
  119. sb.append(KuaiDi100.key);
  120. sb.append("&com=").append(com);
  121. sb.append("&nu=").append(nu);
  122. sb.append("&show=0&muti=1&order=desc");
  123. URL url= new URL(sb.toString());
  124. URLConnection con=url.openConnection();
  125. con.setAllowUserInteraction(false);
  126. InputStream urlStream = url.openStream();
  127. String type = con.guessContentTypeFromStream(urlStream);
  128. String charSet=null;
  129. if (type == null)
  130. type = con.getContentType();
  131. if (type == null || type.trim().length() == 0)
  132. return null;
  133. if(type.indexOf("charset=") > 0)
  134. charSet = type.substring(type.indexOf("charset=") + 8);
  135. byte b[] = new byte[10000];
  136. int numRead = urlStream.read(b);
  137. String content = new String(b, 0, numRead, "UTF-8");
  138. while (numRead != -1) {
  139. numRead = urlStream.read(b);
  140. if (numRead != -1) {
  141. //String newContent = new String(b, 0, numRead);
  142. String newContent = new String(b, 0, numRead, "UTF-8");
  143. content += newContent;
  144. }
  145. }
  146. ret = JSONObject.fromObject(content);
  147. System.out.println(ret);
  148. urlStream.close();
  149. } catch (MalformedURLException e)
  150. {
  151. e.printStackTrace();
  152. } catch (IOException e)
  153. {
  154. e.printStackTrace();
  155. }
  156. return ret;
  157. }
  158. /**
  159. * 该接口使用与收费的快递公司接口查询
  160. * @param com 快递公司代码
  161. * @param nu 快递单号
  162. * @return
  163. */
  164. public static String searchkuaiDiInfo(String com, String nu)
  165. {
  166. String content = "";
  167. try
  168. {
  169. StringBuilder sb = new StringBuilder();
  170. sb.append("http://www.kuaidi100.com/applyurl?key=").append(KuaiDi100.key);
  171. sb.append("&com=").append(com);
  172. sb.append("&nu=").append(nu);
  173. URL url = new URL(sb.toString());
  174. URLConnection con = url.openConnection();
  175. con.setAllowUserInteraction(false);
  176. InputStream urlStream = url.openStream();
  177. byte b[] = new byte[10000];
  178. int numRead = urlStream.read(b);
  179. content = new String(b, 0, numRead);
  180. while (numRead != -1)
  181. {
  182. numRead = urlStream.read(b);
  183. if (numRead != -1)
  184. {
  185. // String newContent = new String(b, 0, numRead);
  186. String newContent = new String(b, 0, numRead, "UTF-8");
  187. content += newContent;
  188. }
  189. }
  190. urlStream.close();
  191. }
  192. catch (Exception e)
  193. {
  194. e.printStackTrace();
  195. System.out.println("快递查询错误");
  196. }
  197. return content;
  198. }
  199. /**物流名称替换**/
  200. public static String replace(String code) {
  201. String name = "";
  202. switch (code){
  203. case "sto":
  204. name= "申通快递";
  205. break;
  206. case "yto":
  207. name= "圆通快递";
  208. break;
  209. case "zto":
  210. name= "中通速递";
  211. break;
  212. case "sf":
  213. name= "顺丰快递";
  214. break;
  215. case "ems":
  216. name= "邮政EMS";
  217. break;
  218. case "eyb":
  219. name= "E邮宝";
  220. break;
  221. case "zjs":
  222. name= "宅急送";
  223. break;
  224. case "yunda":
  225. name= "韵达快递";
  226. break;
  227. case "cces":
  228. name= "cces快递";
  229. break;
  230. case "pick":
  231. name= "上门提货";
  232. break;
  233. case "htky":
  234. name= "汇通快递";
  235. break;
  236. case "ttkdex":
  237. name= "天天快递";
  238. break;
  239. case "stars":
  240. name= "星晨急便";
  241. break;
  242. case "jd":
  243. name= "京东快递";
  244. break;
  245. case "01":
  246. name= "其他";
  247. break;
  248. case "02":
  249. name= "上门送货";
  250. break;
  251. case "longbanwuliu":
  252. name= "龙邦快递";
  253. break;
  254. default:
  255. break;
  256. }
  257. return name;
  258. }
  259. }