In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to realize java express electronic bill printing interface with demo". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Call the express bird API directly.
First of all, log on to the official website of Express Bird to register for http://www.kdniao.com/reg.
Then log in to the express bird official website to authenticate your user information and remember your users ID and API key, which will be used when calling the API in the code. I mainly do a logistics tracking function here, so I need to call the logistics tracking API, so I need to subscribe to this API service first. If you want to call other APIs, you can subscribe accordingly.
Then find here on the official website and click to download the demo of the corresponding language.
In the corresponding express bird background, you can print in batches as follows.
If you want to integrate this printing function into your internal system, you can download the official demo
It's easy to run, just put it into tomcat and run it.
However, demo needs tomcat8.5, so if you need to modify it, you can find one under the .settings folder of the project.
Org.eclipse.wst.common.project.facet.core.xml can just change the tomcat8.5 to the corresponding version.
Official demo code
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; / / whether to preview 0-do not preview 1-Preview / * * @ 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\ ":\" Printer name 2\ "]" 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 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, write ip to death in the background, you can print preview operation.
Printing needs to install the lodop print plug-in. After installation, access to http://localhost:8000/CLodopfuncs.js will have the corresponding control js.
Need the corresponding print plug-in, must have equipment (thermal printer), otherwise I will not travel.
Then, according to the printer model, go to the corresponding official website to download the print driver. After that, perfect, you can print.
Official demo gave it to servlet.
I use SpringMVC to post the code.
Final String EBussinessID = "12677 preview *"; / / kdniao.com EBusinessID final String AppKey = "8e9dcb4b-f0a1-42f6-9d80-372a67f851preview *"; / / kdniao.com AppKey final Integer IsPreview = 1; / / whether to preview 0-not preview 1-Preview @ RequestMapping ("/ getCloudPrintData.do") @ ResponseBody public Map getCloudPrintData (HttpServletRequest request,String OrderCode,String ip) throws Exception {System.out.println ("ok:" + OrderCode+ "- -" + ip) Map map = new HashMap (); List list = new ArrayList (); Map dataMap = new HashMap (); 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 (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; iTunes +) {int val = result [I] & 0xff If (val 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;}
Corresponding html
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) })} Test print page "how to implement java express electronic bill printing interface docking demo" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.