KuaiDi100.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. package com.iamberry.rst.utils;
  2. import com.iamberry.wechat.tools.HttpClient431Util;
  3. import net.sf.json.JSONObject;
  4. import org.jsoup.Jsoup;
  5. import org.jsoup.nodes.Document;
  6. import org.jsoup.nodes.Element;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.stereotype.Component;
  10. import java.io.*;
  11. import java.net.MalformedURLException;
  12. import java.net.URL;
  13. import java.net.URLConnection;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. /**
  17. * Created by Administrator on 2017/8/15.
  18. */
  19. @Component
  20. public class KuaiDi100 {
  21. private static Logger LOGGER = LoggerFactory.getLogger(KuaiDi100.class);
  22. private static String key = "29833628d495d7a5";//授权密匙key 百度的
  23. /*private static String key = "06d6c2ea1bdb4ee9";//授权密匙key 自己的*/
  24. /*<option value="sto">申通快递</option>
  25. <option value="yto">圆通快递</option>
  26. <option value="sf">顺丰快递</option>
  27. <option value="ems">邮政EMS</option>
  28. <option value="zto">中通快递</option>
  29. <option value="zjs">宅急送</option>
  30. <option value="yunda">韵达快递</option>
  31. <option value="cces">cces快递</option>
  32. <option value="pick">上门提货</option>
  33. <option value="htky">汇通快递</option>
  34. <option value="ttkdex">天天快递</option>
  35. <option value="stars">星晨急便</option>
  36. <option value="jd">京东快递</option>
  37. <option value="01">其他</option>
  38. <option value="02">上门送货</option>*/
  39. public String logisticsConversions(String code){
  40. if(code == null || code.equals("")){
  41. return null;
  42. }
  43. switch (code){
  44. case "sto":
  45. code = "shentong";
  46. break;
  47. case "yto":
  48. code = "yuantong";
  49. break;
  50. case "sf":
  51. code = "shunfeng";
  52. break;
  53. case "ems":
  54. code = "ems";
  55. break;
  56. case "eyb":
  57. code = "ems";
  58. break;
  59. case "zto":
  60. code = "zhongtong";
  61. break;
  62. case "zjs":
  63. code = "zhaijisong";
  64. break;
  65. case "yunda":
  66. code = "yunda";
  67. break;
  68. case "cces":
  69. code = "cces";
  70. break;
  71. case "pick":
  72. code = "pick";
  73. break;
  74. case "htky":
  75. code = "huitongkuaidi";
  76. break;
  77. case "ttkdex":
  78. code = "tiantian";
  79. break;
  80. case "stars":
  81. code = "xingchenjibian";
  82. break;
  83. case "jd":
  84. code = "jd";
  85. break;
  86. case "01":
  87. code = "01";
  88. break;
  89. case "02":
  90. code = "01";
  91. break;
  92. case "dbkd":
  93. code = "debangwuliu";
  94. break;
  95. case "lianhaowuliu":
  96. code = "lianhaowuliu";
  97. break;
  98. }
  99. return code;
  100. }
  101. /**
  102. * 快递对应代码
  103. * EMS ems/eyb
  104. * 申通快递 shentong
  105. * 顺丰速运 shunfeng
  106. * 圆通速递 yuantong
  107. * 韵达快运 yunda
  108. * 中通快递 zhongtong
  109. * 百世快递 huitongkuaidi
  110. * 天天快递 tiantian
  111. * 宅急送 zhaijisong
  112. * 邮政国内包裹 youzhengguonei
  113. * 邮政国际包裹 youzhengguoji
  114. * EMS国际快递 emsguoji
  115. */
  116. public static void main(String[] agrs){
  117. KuaiDi100 kuaidi = new KuaiDi100();
  118. System.out.println(kuaidi.getExpressInfo("ems","9754210807242"));
  119. }
  120. /**
  121. * 查询快递信息
  122. * @param com 快递公司代码
  123. * @param nu 快递单号
  124. * @return
  125. */
  126. public JSONObject getExpressInfo(String com , String nu){
  127. JSONObject ret = null;
  128. try
  129. {
  130. com = logisticsConversions(com);
  131. StringBuilder sb = new StringBuilder();
  132. sb.append("http://api.kuaidi100.com/api?id=");
  133. sb.append(KuaiDi100.key);
  134. sb.append("&com=").append(com);
  135. sb.append("&nu=").append(nu);
  136. sb.append("&show=0&muti=1&order=desc");
  137. URL url= new URL(sb.toString());
  138. URLConnection con=url.openConnection();
  139. con.setAllowUserInteraction(false);
  140. InputStream urlStream = url.openStream();
  141. String type = con.guessContentTypeFromStream(urlStream);
  142. String charSet=null;
  143. if (type == null)
  144. type = con.getContentType();
  145. if (type == null || type.trim().length() == 0)
  146. return null;
  147. if(type.indexOf("charset=") > 0)
  148. charSet = type.substring(type.indexOf("charset=") + 8);
  149. byte b[] = new byte[10000];
  150. int numRead = urlStream.read(b);
  151. String content = new String(b, 0, numRead, "UTF-8");
  152. while (numRead != -1) {
  153. numRead = urlStream.read(b);
  154. if (numRead != -1) {
  155. //String newContent = new String(b, 0, numRead);
  156. String newContent = new String(b, 0, numRead, "UTF-8");
  157. content += newContent;
  158. }
  159. }
  160. content = content.replaceAll(":null", ":\"\"");
  161. ret = JSONObject.fromObject(content);
  162. System.out.println(ret);
  163. urlStream.close();
  164. } catch (MalformedURLException e)
  165. {
  166. e.printStackTrace();
  167. } catch (IOException e)
  168. {
  169. e.printStackTrace();
  170. }
  171. return ret;
  172. }
  173. /**
  174. * 该接口使用与收费的快递公司接口查询
  175. * @param com 快递公司代码
  176. * @param nu 快递单号
  177. * @return
  178. */
  179. public static String searchkuaiDiInfo(String com, String nu)
  180. {
  181. String content = "";
  182. try
  183. {
  184. StringBuilder sb = new StringBuilder();
  185. sb.append("http://www.kuaidi100.com/applyurl?key=").append(KuaiDi100.key);
  186. sb.append("&com=").append(com);
  187. sb.append("&nu=").append(nu);
  188. URL url = new URL(sb.toString());
  189. URLConnection con = url.openConnection();
  190. con.setAllowUserInteraction(false);
  191. InputStream urlStream = url.openStream();
  192. byte b[] = new byte[10000];
  193. int numRead = urlStream.read(b);
  194. content = new String(b, 0, numRead);
  195. while (numRead != -1)
  196. {
  197. numRead = urlStream.read(b);
  198. if (numRead != -1)
  199. {
  200. // String newContent = new String(b, 0, numRead);
  201. String newContent = new String(b, 0, numRead, "UTF-8");
  202. content += newContent;
  203. }
  204. }
  205. urlStream.close();
  206. }
  207. catch (Exception e)
  208. {
  209. e.printStackTrace();
  210. System.out.println("快递查询错误");
  211. }
  212. return content;
  213. }
  214. /**物流名称替换**/
  215. public static String replace(String code) {
  216. String name = "";
  217. switch (code){
  218. case "sto":
  219. name= "申通快递";
  220. break;
  221. case "yto":
  222. name= "圆通快递";
  223. break;
  224. case "zto":
  225. name= "中通速递";
  226. break;
  227. case "sf":
  228. name= "顺丰快递";
  229. break;
  230. case "ems":
  231. name= "邮政EMS";
  232. break;
  233. case "eyb":
  234. name= "E邮宝";
  235. break;
  236. case "zjs":
  237. name= "宅急送";
  238. break;
  239. case "yunda":
  240. name= "韵达快递";
  241. break;
  242. case "cces":
  243. name= "cces快递";
  244. break;
  245. case "pick":
  246. name= "上门提货";
  247. break;
  248. case "htky":
  249. name= "汇通快递";
  250. break;
  251. case "ttkdex":
  252. name= "天天快递";
  253. break;
  254. case "stars":
  255. name= "星晨急便";
  256. break;
  257. case "jd":
  258. name= "京东快递";
  259. break;
  260. case "01":
  261. name= "其他";
  262. break;
  263. case "02":
  264. name= "上门送货";
  265. break;
  266. case "lbwl":
  267. name= "龙邦快递";
  268. break;
  269. default:
  270. break;
  271. case "dbkd":
  272. name = "德邦快递";
  273. break;
  274. }
  275. return name;
  276. }
  277. /**
  278. * 获取快递公司
  279. * @param firm
  280. * @return
  281. */
  282. public String getPostFirm(String firm) {
  283. String postFirm = "";
  284. switch (firm) {
  285. case "sto":postFirm = "申通快递";break;
  286. case "yto":postFirm = "圆通快递";break;
  287. case "sf":postFirm = "顺丰快递";break;
  288. case "ems":postFirm = "邮政EMS";break;
  289. case "zto":postFirm = "中通快递";break;
  290. case "zjs":postFirm = "宅急送";break;
  291. case "yunda":postFirm = "韵达快递";break;
  292. case "cces":postFirm = "cces快递";break;
  293. case "pick":postFirm = "上门提货";break;
  294. case "htky":postFirm = "汇通快递";break;
  295. case "ttkdex":postFirm = "天天快递";break;
  296. case "stars":postFirm = "星晨急便";break;
  297. case "jd":postFirm = "京东快递";break;
  298. case "01":postFirm = "其他";break;
  299. case "02":postFirm = "上门送货";break;
  300. case "dbkd":postFirm = "德邦快递";break;
  301. default:postFirm = "申通快递";break;
  302. }
  303. return postFirm;
  304. }
  305. /**
  306. * 将快递100的物流公司编号转为rst的物流编号
  307. * @return
  308. */
  309. private String kuaidiToRstCode(String company){
  310. String postFirm = "";
  311. switch (company){
  312. case "shentong":postFirm="STO";break;
  313. case "debangwuliu":postFirm="DBL";break;
  314. case "youzhengguonei":postFirm="YZBK";break; //9719606885204 省外
  315. case "ems":postFirm="EMS";break; //1170100776910 省内
  316. case "shunfeng":postFirm="SF";break;
  317. case "jd":postFirm="jd";break;
  318. case "youshuwuliu":postFirm="youshuwuliu";break; //优速
  319. case "zhongtong":postFirm="zto";break; //优速
  320. default:
  321. postFirm = company;
  322. LOGGER.error("未匹配到物流公司");
  323. break;
  324. }
  325. return postFirm;
  326. }
  327. /**
  328. * 快递一百
  329. * 根据物流单号查询快递公司--仅能查询快递公司,不能查询快递流程
  330. * @param code
  331. * @return
  332. */
  333. public String selectCourierCompany(String code) {
  334. String courierCompang="";
  335. // Map<String,Object> requestMap = new HashMap();
  336. // requestMap.put("LogisticCode",code);
  337. try {
  338. Map<String, String> params = new HashMap<String, String>();
  339. params.put("nu", code);
  340. String url = "http://api.open.baidu.com/pae/common/api/Redirect";
  341. // 发送请求
  342. String html = HttpClient431Util.doGet(params, url);
  343. Document doc = Jsoup.parse(html);
  344. Element content = doc.getElementById("companyCode");
  345. String value = content.val();
  346. if(value == null || "".equals(value)){
  347. LOGGER.info("=========快递单号:"+code+";查询不到快递公司");
  348. }else{
  349. value = kuaidiToRstCode(value); //转换物流方式
  350. LOGGER.info("=========快递单号:"+code+";查询成功,快递公司:"+code+":"+value);
  351. }
  352. } catch (Exception e) {
  353. e.printStackTrace();
  354. LOGGER.info("=========快递单号:"+code+";处理报错");
  355. }
  356. return courierCompang;
  357. }
  358. /**
  359. * 写入文件
  360. * @param path
  361. * @param s
  362. */
  363. public static void writeTxt(String path,String s){
  364. File file = new File(path);
  365. try{
  366. FileWriter fw = new FileWriter(file, true);
  367. if(!file.exists()){
  368. file.createNewFile();
  369. }
  370. BufferedWriter bw = new BufferedWriter(fw);
  371. PrintWriter pw = new PrintWriter(bw);
  372. pw.println(s);
  373. pw.flush();
  374. fw.flush();
  375. pw.close();
  376. fw.close();
  377. }catch (Exception e){
  378. e.printStackTrace();
  379. }
  380. }
  381. }