手写快递单太麻烦了,公司的订单又多,每次都写不过来,还容易出错,直接电子化在打印快递单就快了,不过现在有些人不懂怎么批量对接多个快递的电子面单,这里就给大家讲解讲解,希望对大家有用。
首先需要提前购买电子面单单号才能打印,这里给大家介绍对接电子面单接口对接和在线申请电子面单账号的方法,
快递鸟有电子面单api,后台系统直接对接很是方便,不过也遇到了好些问题。
不难是不难,但是遇到的坑着实是不少,特此记录一下。
快递鸟电子面单API地址:http://www.kdniao.com/api-eorder
都是在正式环境下,申请对应的商户id等一系列东西。
在对应的快递鸟后台,可以进行如下的批量打印。
官方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; } }
想把这个打印功能集成到自己内部系统,可以下载官方的demo
跑起来挺容易的,直接放入tomcat运行就可以了
不过demo需要tomcat8.5,需要修改的话找到项目的.settings文件夹下有一个
org.eclipse.wst.common.project.facet.core.xml 将tomcat8.5修改为对应的版本就可以了。
官方demo代码
一、进入快递鸟网站,注册并登录,然后进行实名认证
申请地址:http://www.kdniao.com/reg
二、申请电子面单账号:电子面单→我的快递公司→申请新的快递公司→
三、填写发货地址(必需填写完整省市区以及详细地址,系统才能自动匹配出对应的揽件网点(申请页面下拉框选择)--建议客户最好先了解清楚与自己合作的线下网点的名称,方便申请时选择)及联系人(用于短信接收电子面单账号)。
然后等待审核就好
四、向线下网点充值电子面单打印单量,这一步是唯一需要下操作的步骤
五、然后进行电子面单接口对接,在网站下载接口文档和接口demo,demo里没有可用的接口ID和接口秘钥,每个账号都有独立的ID和KEY,在登录后台可以查询,替换一下就可以调用demo了
六、对接后可以在调试平台调试,上面有正确的请求报文和报文,可以和自己的报文进行校验
七、等账号审核通过并充值单号后就可以打印电子面单了,接口会返回HTML格式的电子面单,就可以打印了
注意要用自己的秘钥换掉原始秘钥,要不然运行不了的