您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“怎么實現(xiàn)java快遞電子面單打印接口對接demo”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
直接調(diào)用快遞鳥API。
首先登陸快遞鳥官網(wǎng)進行會員注冊http://www.kdniao.com/reg,
然后登錄快遞鳥官網(wǎng)后臺認(rèn)證自己的用戶信息,記住自己的用戶ID和API key,代碼里調(diào)用接口時會使用到。我這里主要做個物流跟蹤的功能,所以我需要調(diào)用的物流跟蹤API,所以我需要先訂閱這個API的服務(wù)。大家如果想調(diào)用別的接口就相應(yīng)的去訂閱就可以了。
然后在官網(wǎng)找到這里 點擊下載相應(yīng)語言的demo
在對應(yīng)的快遞鳥后臺,可以進行如下的批量打印。
想把這個打印功能集成到自己內(nèi)部系統(tǒng),可以下載官方的demo
跑起來挺容易的,直接放入tomcat運行就可以了
不過demo需要tomcat8.5,需要修改的話找到項目的.settings文件夾下有一個
org.eclipse.wst.common.project.facet.core.xml 將tomcat8.5修改為對應(yīng)的版本就可以了。
官方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; //是否預(yù)覽 0-不預(yù)覽 1-預(yù)覽 /** * @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參數(shù),獲取這個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進行打印,這個時候提交總是說數(shù)據(jù)驗證不通過,并不是簽名問題,而是ip問題。
因為上一個方法只要是在局域網(wǎng)下面,即使能訪問外網(wǎng),也沒用,獲取到的ip永遠(yuǎn)都是內(nèi)網(wǎng)ip
有一種方式可以在本地驗證通過,那就是直接百度ip,只有會有自己的ip地址
OK,在后臺將ip寫死,就可以進行打印預(yù)覽操作了。
打印需要安裝lodop打印插件,安裝完成之后訪問 http://localhost:8000/CLodopfuncs.js 會有相應(yīng)的控件js
需要對應(yīng)的打印插件,必須要有設(shè)備(熱敏打印機),要不我也不至于出差了。
之后根據(jù)打印機型號,進入對應(yīng)的官網(wǎng)下載打印驅(qū)動。之后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; //是否預(yù)覽 0-不預(yù)覽 1-預(yù)覽 @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; }
對應(yīng)的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">
“怎么實現(xiàn)java快遞電子面單打印接口對接demo”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。