您好,登錄后才能下訂單哦!
本篇內容介紹了“怎么實現java快遞電子面單打印接口對接demo”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
直接調用快遞鳥API。
首先登陸快遞鳥官網進行會員注冊http://www.kdniao.com/reg,
然后登錄快遞鳥官網后臺認證自己的用戶信息,記住自己的用戶ID和API key,代碼里調用接口時會使用到。我這里主要做個物流跟蹤的功能,所以我需要調用的物流跟蹤API,所以我需要先訂閱這個API的服務。大家如果想調用別的接口就相應的去訂閱就可以了。
然后在官網找到這里 點擊下載相應語言的demo
在對應的快遞鳥后臺,可以進行如下的批量打印。
想把這個打印功能集成到自己內部系統,可以下載官方的demo
跑起來挺容易的,直接放入tomcat運行就可以了
不過demo需要tomcat8.5,需要修改的話找到項目的.settings文件夾下有一個
org.eclipse.wst.common.project.facet.core.xml 將tomcat8.5修改為對應的版本就可以了。
官方demo代碼
package cc.kdniao.api; import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.security.MessageDigest; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sun.xml.internal.messaging.saaj.util.Base64; /** * Servlet implementation class printOrder */ @WebServlet("/printOrder") public class printOrder extends HttpServlet { private static final long serialVersionUID = 1L; final String EBussinessID = "";//kdniao.com EBusinessID final String AppKey = ""; //kdniao.com AppKey final Integer IsPreview = 0; //是否預覽 0-不預覽 1-預覽 /** * @see HttpServlet#HttpServlet() */ public printOrder() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //response.getWriter().append("Served at: ").append(request.getContextPath()); PrintWriter print = response.getWriter(); String jsonResult = ""; try { String ip = getIpAddress(request); jsonResult = getPrintParam(ip); } catch (Exception e) { //write log } print.println(jsonResult); print.flush(); print.close(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, UnsupportedEncodingException { // TODO Auto-generated method stub response.setContentType(""); PrintWriter print = response.getWriter(); String jsonResult = ""; try { String ip = getIpAddress(request); jsonResult = getPrintParam(ip); } catch (Exception e) { //wirte log } print.println(jsonResult); print.flush(); print.close(); } /** * get print order param to json string * @return * * @throws Exception */ private String getPrintParam(String ip) throws Exception { String data = "[{\"OrderCode\":\"234351215333113311353\",\"PortName\":\"SF\"},{\"OrderCode\":\"234351215333113311354\",\"PortName\":\"打印機名稱二\"}]"; String result = "{\"RequestData\": \"" + URLEncoder.encode(data, "UTF-8") + "\", \"EBusinessID\":\"" + EBussinessID + "\", \"DataSign\":\"" + encrpy(ip + data, AppKey) + "\", \"IsPreview\":\"" + IsPreview + "\"}"; return result; } private String md5(String str, String charset) throws Exception { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(str.getBytes(charset)); byte[] result = md.digest(); StringBuffer sb = new StringBuffer(32); for (int i = 0; i < result.length; i++) { int val = result[i] & 0xff; if (val <= 0xf) { sb.append("0"); } sb.append(Integer.toHexString(val)); } return sb.toString().toLowerCase(); } private String encrpy(String content, String key) throws UnsupportedEncodingException, Exception { String charset = "UTF-8"; return new String(Base64.encode(md5(content + key, charset).getBytes(charset))); } /** * 獲取請求主機IP地址,如果通過代理進來,則透過防火墻獲取真實IP地址; * * @param request * @return * @throws IOException */ public final static String getIpAddress(HttpServletRequest request) throws IOException { // 獲取請求主機IP地址,如果通過代理進來,則透過防火墻獲取真實IP地址 String ip = request.getHeader("X-Forwarded-For"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_CLIENT_IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } } else if (ip.length() > 15) { String[] ips = ip.split(","); for (int index = 0; index < ips.length; index++) { String strIp = (String) ips[index]; if (!("unknown".equalsIgnoreCase(strIp))) { ip = strIp; break; } } } return ip; } }
但是,需要注意的是,API有一個ip參數,獲取這個ip快遞鳥提供的方法是如下
// 獲取請求主機IP地址,如果通過代理進來,則透過防火墻獲取真實IP地址 public final static String getIpAddress(HttpServletRequest request) throws IOException { String ip = request.getHeader("X-Forwarded-For"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_CLIENT_IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } } else if (ip.length() > 15) { String[] ips = ip.split(","); for (int index = 0; index < ips.length; index++) { String strIp = (String) ips[index]; if (!("unknown".equalsIgnoreCase(strIp))) { ip = strIp; break; } } } return ip; }
注意這個方法是沒有問題的,但是你如果直接輸入快遞單號OrderCode進行打印,這個時候提交總是說數據驗證不通過,并不是簽名問題,而是ip問題。
因為上一個方法只要是在局域網下面,即使能訪問外網,也沒用,獲取到的ip永遠都是內網ip
有一種方式可以在本地驗證通過,那就是直接百度ip,只有會有自己的ip地址
OK,在后臺將ip寫死,就可以進行打印預覽操作了。
打印需要安裝lodop打印插件,安裝完成之后訪問 http://localhost:8000/CLodopfuncs.js 會有相應的控件js
需要對應的打印插件,必須要有設備(熱敏打印機),要不我也不至于出差了。
之后根據打印機型號,進入對應的官網下載打印驅動。之后perfect,就可以進行打印了。
官方demo給的是servlet
我使用的是SpringMVC,將代碼貼出。
final String EBussinessID = "12677**";//kdniao.com EBusinessID final String AppKey = "8e9dcb4b-f0a1-42f6-9d80-372a67f851**"; //kdniao.com AppKey final Integer IsPreview = 1; //是否預覽 0-不預覽 1-預覽 @RequestMapping("/getCloudPrintData.do") @ResponseBody public Map<String, Object> getCloudPrintData(HttpServletRequest request,String OrderCode,String ip) throws Exception{ System.out.println("ok:"+OrderCode+"---"+ip); Map<String,Object> map = new HashMap<String, Object>(); List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); Map<String,Object> dataMap = new HashMap<String, Object>(); dataMap.put("OrderCode", OrderCode); dataMap.put("PortName", "Xprinter XP-DT108A LABEL"); list.add(dataMap); String jsonString = JSONArray.fromObject(dataMap).toString(); map.put("RequestData", URLEncoder.encode(jsonString, "UTF-8")); if(StringUtils.isEmpty(ip)) { ip = getIpAddress(request); } map.put("DataSign",encrpy(ip + jsonString, AppKey)); System.out.println("map:"+map); return map; } @RequestMapping("/getIpAddressByJava.do") @ResponseBody public String getIpAddressByJava(HttpServletRequest request) throws IOException { return getIpAddress(request); } private String encrpy(String content, String key) throws UnsupportedEncodingException, Exception { String charset = "UTF-8"; return new String(Base64.encode(md5(content + key, charset).getBytes(charset))); } private String md5(String str, String charset) throws Exception { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(str.getBytes(charset)); byte[] result = md.digest(); StringBuffer sb = new StringBuffer(32); for (int i = 0; i < result.length; i++) { int val = result[i] & 0xff; if (val <= 0xf) { sb.append("0"); } sb.append(Integer.toHexString(val)); } return sb.toString().toLowerCase(); } // 獲取請求主機IP地址,如果通過代理進來,則透過防火墻獲取真實IP地址 public final static String getIpAddress(HttpServletRequest request) throws IOException { String ip = request.getHeader("X-Forwarded-For"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_CLIENT_IP"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("HTTP_X_FORWARDED_FOR"); } if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } } else if (ip.length() > 15) { String[] ips = ip.split(","); for (int index = 0; index < ips.length; index++) { String strIp = (String) ips[index]; if (!("unknown".equalsIgnoreCase(strIp))) { ip = strIp; break; } } } return ip; }
對應的html
<body> <script type="text/javascript"> function fasong(){ var orderNo = $("#orderNo").val(); var ipAddress = $("#ipAddress").val(); $.ajax({ url:'/jeeadmin/jeecms/getCloudPrintData.do', data:{OrderCode:orderNo,ip:ipAddress}, success:function(data){ $("#RequestData").val(data.RequestData) $("#DataSign").val(data.DataSign) } }) } function getIp(){ $.ajax({ url:'/jeeadmin/jeecms/getIpAddressByJava.do', success:function(data){ alert(data); } }) } </script> <h2>測試打印頁面</h2> <div id="head"></div> <div> <input type="text">
“怎么實現java快遞電子面單打印接口對接demo”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。