您好,登录后才能下订单哦!
本篇内容介绍了“怎么实现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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。