123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- package com.iamberry.rst.utils;
- import com.iamberry.wechat.tools.HttpClient431Util;
- import net.sf.json.JSONObject;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import org.jsoup.nodes.Element;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Component;
- import java.io.*;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLConnection;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * Created by Administrator on 2017/8/15.
- */
- @Component
- public class KuaiDi100 {
- private static Logger LOGGER = LoggerFactory.getLogger(KuaiDi100.class);
- private static String key = "29833628d495d7a5";//授权密匙key 百度的
- /*private static String key = "06d6c2ea1bdb4ee9";//授权密匙key 自己的*/
- /*<option value="sto">申通快递</option>
- <option value="yto">圆通快递</option>
- <option value="sf">顺丰快递</option>
- <option value="ems">邮政EMS</option>
- <option value="zto">中通快递</option>
- <option value="zjs">宅急送</option>
- <option value="yunda">韵达快递</option>
- <option value="cces">cces快递</option>
- <option value="pick">上门提货</option>
- <option value="htky">汇通快递</option>
- <option value="ttkdex">天天快递</option>
- <option value="stars">星晨急便</option>
- <option value="jd">京东快递</option>
- <option value="01">其他</option>
- <option value="02">上门送货</option>*/
- public String logisticsConversions(String code){
- if(code == null || code.equals("")){
- return null;
- }
- switch (code){
- case "sto":
- code = "shentong";
- break;
- case "yto":
- code = "yuantong";
- break;
- case "sf":
- code = "shunfeng";
- break;
- case "ems":
- code = "ems";
- break;
- case "eyb":
- code = "ems";
- break;
- case "zto":
- code = "zhongtong";
- break;
- case "zjs":
- code = "zhaijisong";
- break;
- case "yunda":
- code = "yunda";
- break;
- case "cces":
- code = "cces";
- break;
- case "pick":
- code = "pick";
- break;
- case "htky":
- code = "huitongkuaidi";
- break;
- case "ttkdex":
- code = "tiantian";
- break;
- case "stars":
- code = "xingchenjibian";
- break;
- case "jd":
- code = "jd";
- break;
- case "01":
- code = "01";
- break;
- case "02":
- code = "01";
- break;
- case "dbkd":
- code = "debangwuliu";
- break;
- case "lianhaowuliu":
- code = "lianhaowuliu";
- break;
- }
- return code;
- }
- /**
- * 快递对应代码
- * EMS ems/eyb
- * 申通快递 shentong
- * 顺丰速运 shunfeng
- * 圆通速递 yuantong
- * 韵达快运 yunda
- * 中通快递 zhongtong
- * 百世快递 huitongkuaidi
- * 天天快递 tiantian
- * 宅急送 zhaijisong
- * 邮政国内包裹 youzhengguonei
- * 邮政国际包裹 youzhengguoji
- * EMS国际快递 emsguoji
- */
- public static void main(String[] agrs){
- KuaiDi100 kuaidi = new KuaiDi100();
- System.out.println(kuaidi.getExpressInfo("ems","9754210807242"));
- }
- /**
- * 查询快递信息
- * @param com 快递公司代码
- * @param nu 快递单号
- * @return
- */
- public JSONObject getExpressInfo(String com , String nu){
- JSONObject ret = null;
- try
- {
- com = logisticsConversions(com);
- StringBuilder sb = new StringBuilder();
- sb.append("http://api.kuaidi100.com/api?id=");
- sb.append(KuaiDi100.key);
- sb.append("&com=").append(com);
- sb.append("&nu=").append(nu);
- sb.append("&show=0&muti=1&order=desc");
- URL url= new URL(sb.toString());
- URLConnection con=url.openConnection();
- con.setAllowUserInteraction(false);
- InputStream urlStream = url.openStream();
- String type = con.guessContentTypeFromStream(urlStream);
- String charSet=null;
- if (type == null)
- type = con.getContentType();
- if (type == null || type.trim().length() == 0)
- return null;
- if(type.indexOf("charset=") > 0)
- charSet = type.substring(type.indexOf("charset=") + 8);
- byte b[] = new byte[10000];
- int numRead = urlStream.read(b);
- String content = new String(b, 0, numRead, "UTF-8");
- while (numRead != -1) {
- numRead = urlStream.read(b);
- if (numRead != -1) {
- //String newContent = new String(b, 0, numRead);
- String newContent = new String(b, 0, numRead, "UTF-8");
- content += newContent;
- }
- }
- content = content.replaceAll(":null", ":\"\"");
- ret = JSONObject.fromObject(content);
- System.out.println(ret);
- urlStream.close();
- } catch (MalformedURLException e)
- {
- e.printStackTrace();
- } catch (IOException e)
- {
- e.printStackTrace();
- }
- return ret;
- }
- /**
- * 该接口使用与收费的快递公司接口查询
- * @param com 快递公司代码
- * @param nu 快递单号
- * @return
- */
- public static String searchkuaiDiInfo(String com, String nu)
- {
- String content = "";
- try
- {
- StringBuilder sb = new StringBuilder();
- sb.append("http://www.kuaidi100.com/applyurl?key=").append(KuaiDi100.key);
- sb.append("&com=").append(com);
- sb.append("&nu=").append(nu);
- URL url = new URL(sb.toString());
- URLConnection con = url.openConnection();
- con.setAllowUserInteraction(false);
- InputStream urlStream = url.openStream();
- byte b[] = new byte[10000];
- int numRead = urlStream.read(b);
- content = new String(b, 0, numRead);
- while (numRead != -1)
- {
- numRead = urlStream.read(b);
- if (numRead != -1)
- {
- // String newContent = new String(b, 0, numRead);
- String newContent = new String(b, 0, numRead, "UTF-8");
- content += newContent;
- }
- }
- urlStream.close();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- System.out.println("快递查询错误");
- }
- return content;
- }
- /**物流名称替换**/
- public static String replace(String code) {
- String name = "";
- switch (code){
- case "sto":
- name= "申通快递";
- break;
- case "yto":
- name= "圆通快递";
- break;
- case "zto":
- name= "中通速递";
- break;
- case "sf":
- name= "顺丰快递";
- break;
- case "ems":
- name= "邮政EMS";
- break;
- case "eyb":
- name= "E邮宝";
- break;
- case "zjs":
- name= "宅急送";
- break;
- case "yunda":
- name= "韵达快递";
- break;
- case "cces":
- name= "cces快递";
- break;
- case "pick":
- name= "上门提货";
- break;
- case "htky":
- name= "汇通快递";
- break;
- case "ttkdex":
- name= "天天快递";
- break;
- case "stars":
- name= "星晨急便";
- break;
- case "jd":
- name= "京东快递";
- break;
- case "01":
- name= "其他";
- break;
- case "02":
- name= "上门送货";
- break;
- case "lbwl":
- name= "龙邦快递";
- break;
- default:
- break;
- case "dbkd":
- name = "德邦快递";
- break;
- }
- return name;
- }
- /**
- * 获取快递公司
- * @param firm
- * @return
- */
- public String getPostFirm(String firm) {
- String postFirm = "";
- switch (firm) {
- case "sto":postFirm = "申通快递";break;
- case "yto":postFirm = "圆通快递";break;
- case "sf":postFirm = "顺丰快递";break;
- case "ems":postFirm = "邮政EMS";break;
- case "zto":postFirm = "中通快递";break;
- case "zjs":postFirm = "宅急送";break;
- case "yunda":postFirm = "韵达快递";break;
- case "cces":postFirm = "cces快递";break;
- case "pick":postFirm = "上门提货";break;
- case "htky":postFirm = "汇通快递";break;
- case "ttkdex":postFirm = "天天快递";break;
- case "stars":postFirm = "星晨急便";break;
- case "jd":postFirm = "京东快递";break;
- case "01":postFirm = "其他";break;
- case "02":postFirm = "上门送货";break;
- case "dbkd":postFirm = "德邦快递";break;
- default:postFirm = "申通快递";break;
- }
- return postFirm;
- }
- /**
- * 将快递100的物流公司编号转为rst的物流编号
- * @return
- */
- private String kuaidiToRstCode(String company){
- String postFirm = "";
- switch (company){
- case "shentong":postFirm="STO";break;
- case "debangwuliu":postFirm="DBL";break;
- case "youzhengguonei":postFirm="YZBK";break; //9719606885204 省外
- case "ems":postFirm="EMS";break; //1170100776910 省内
- case "shunfeng":postFirm="SF";break;
- case "jd":postFirm="jd";break;
- case "youshuwuliu":postFirm="youshuwuliu";break; //优速
- case "zhongtong":postFirm="zto";break; //优速
- default:
- postFirm = company;
- LOGGER.error("未匹配到物流公司");
- break;
- }
- return postFirm;
- }
- /**
- * 快递一百
- * 根据物流单号查询快递公司--仅能查询快递公司,不能查询快递流程
- * @param code
- * @return
- */
- public String selectCourierCompany(String code) {
- String courierCompang="";
- // Map<String,Object> requestMap = new HashMap();
- // requestMap.put("LogisticCode",code);
- try {
- Map<String, String> params = new HashMap<String, String>();
- params.put("nu", code);
- String url = "http://api.open.baidu.com/pae/common/api/Redirect";
- // 发送请求
- String html = HttpClient431Util.doGet(params, url);
- Document doc = Jsoup.parse(html);
- Element content = doc.getElementById("companyCode");
- String value = content.val();
- if(value == null || "".equals(value)){
- LOGGER.info("=========快递单号:"+code+";查询不到快递公司");
- }else{
- value = kuaidiToRstCode(value); //转换物流方式
- LOGGER.info("=========快递单号:"+code+";查询成功,快递公司:"+code+":"+value);
- }
- } catch (Exception e) {
- e.printStackTrace();
- LOGGER.info("=========快递单号:"+code+";处理报错");
- }
- return courierCompang;
- }
- /**
- * 写入文件
- * @param path
- * @param s
- */
- public static void writeTxt(String path,String s){
- File file = new File(path);
- try{
- FileWriter fw = new FileWriter(file, true);
- if(!file.exists()){
- file.createNewFile();
- }
- BufferedWriter bw = new BufferedWriter(fw);
- PrintWriter pw = new PrintWriter(bw);
- pw.println(s);
- pw.flush();
- fw.flush();
- pw.close();
- fw.close();
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }
|