手寫快遞單太麻煩了,公司的訂單又多,每次都寫不過來,還容易出錯,直接電子化在打印快遞單就快了,不過如今有些人不懂怎麼批量對接多個快遞的電子面單,這裏就給你們講解講解,但願對你們有用。java
首先須要提早購買電子面單單號才能打印,這裏給你們介紹對接電子面單接口對接和在線申請電子面單帳號的方法,json
快遞鳥有電子面單api,後臺系統直接對接非常方便,不過也遇到了好些問題。api
不難是不難,可是遇到的坑着實是很多,特此記錄一下。tomcat
快遞鳥電子面單API地址:http://www.kdniao.com/api-eorderapp
都是在正式環境下,申請對應的商戶id等一系列東西。eclipse
在對應的快遞鳥後臺,能夠進行以下的批量打印。網站
官方demo:spa
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; } }
想把這個打印功能集成到本身內部系統,能夠下載官方的demo.net
跑起來挺容易的,直接放入tomcat運行就能夠了3d
不過demo須要tomcat8.5,須要修改的話找到項目的.settings文件夾下有一個
org.eclipse.wst.common.project.facet.core.xml 將tomcat8.5修改成對應的版本就能夠了。
官方demo代碼
1、進入快遞鳥網站,註冊並登陸,而後進行實名認證
申請地址:http://www.kdniao.com/reg
2、申請電子面單帳號:電子面單→個人快遞公司→申請新的快遞公司→
3、填寫發貨地址(必需填寫完整省市區以及詳細地址,系統才能自動匹配出對應的攬件網點(申請頁面下拉框選擇)--建議客戶最好先了解清楚與本身合做的線下網點的名稱,方便申請時選擇)及聯繫人(用於短信接收電子面單帳號)。
而後等待審覈就好
4、向線下網點充值電子面單打印單量,這一步是惟一須要下操做的步驟
5、而後進行電子面單接口對接,在網站下載接口文檔和接口demo,demo裏沒有可用的接口ID和接口祕鑰,每一個帳號都有獨立的ID和KEY,在登陸後臺能夠查詢,替換一下就能夠調用demo了
6、對接後能夠在調試平臺調試,上面有正確的請求報文和報文,能夠和本身的報文進行校驗
7、等帳號審覈經過並充值單號後就能夠打印電子面單了,接口會返回HTML格式的電子面單,就能夠打印了
注意要用本身的祕鑰換掉原始祕鑰,要否則運行不了的