一、源码简介
本源码实现打印终端(门店)将门店销售报表等数据自动同步到总部或区域分管部门的打印服务器进行打印,本项目工程分别包含门户终端程序和服务器程序部分 - UploadClient.java和UploadServer.java,具体目录结构如下图所示,查看GitHub源码(https://github.com/xwooduser/XWPrintServer)
UploadClient.java
import java.io.BufferedReader;@b@import java.io.IOException;@b@import java.io.InputStreamReader;@b@import java.io.PrintWriter;@b@import java.net.MalformedURLException;@b@import java.net.Socket;@b@import java.net.URL;@b@import java.net.UnknownHostException;@b@@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@@b@import com.xwood.ps.service.Download;@b@import com.xwood.ps.util.PropertieFileReader;@b@@b@/**@b@ * 客户端@b@ */@b@public class UploadClient extends Thread@b@{@b@ public Log log = LogFactory.getLog(getClass());@b@ private static boolean startup = true;@b@ private static String url;@b@ private static int port;@b@ private static Socket socket = null;@b@ private BufferedReader in;@b@ private PrintWriter out;@b@ @b@ private UploadClient(){}@b@ @b@ public void run()@b@ {@b@ try @b@ {@b@ while(startup)@b@ {@b@ System.out.println("客户端开始接收"+url+":"+port+"的信息...");@b@ socket = new Socket(url,port);@b@ @b@ in = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8"));@b@ String filename = in.readLine();@b@ System.out.println(filename);@b@ out = new PrintWriter(socket.getOutputStream(),true);@b@ @b@ if(filename != null && filename.length() > 4 && @b@ (".xls".equalsIgnoreCase(filename.substring(filename.length()-4)) || ".xlsx".equalsIgnoreCase(filename.substring(filename.length()-5))))@b@ {@b@ try@b@ {@b@ System.out.println(PropertieFileReader.getString("downloadUrl")+filename);@b@ Download d = new Download(new URL(PropertieFileReader.getString("downloadUrl")+filename),out);@b@ }@b@ catch (MalformedURLException e) @b@ {@b@ e.printStackTrace();@b@ }@b@ } @b@ else@b@ {@b@ System.out.println("服务器端传过来的文件名不是excel格式,不予以下载");@b@ log.error("服务器端传过来的文件名不是excel格式,不予以下载");@b@ }@b@ out.close();@b@ in.close();@b@ socket.close();@b@ @b@ }@b@ }@b@ catch (IOException e)@b@ {@b@ // 写入错误日志@b@ System.out.println("IO流错误:"+e.toString());@b@ e.printStackTrace();@b@ }@b@ }@b@ @b@ /**@b@ * @param url1 服务器端地址@b@ * @param port1 端口@b@ * 开启客户端@b@ */@b@ public static void startup(String url1,int port1)@b@ { @b@ @b@ url = url1;@b@ port = port1;@b@ startup = true;@b@ System.out.println("服务器端地址:"+url+":"+port);@b@ UploadClient client = new UploadClient();@b@ //socket = new Socket(url,port);@b@ client.start();@b@ @b@ }@b@ @b@ /**@b@ * 关闭服务器端@b@ */@b@ public static void shutdown()@b@ { @b@ try@b@ {@b@ startup = false;@b@ socket.close();@b@ }@b@ catch (IOException e)@b@ {@b@ e.printStackTrace();@b@ }@b@ }@b@ @b@ /**@b@ * @return 返回客户端启动状态@b@ */@b@ public static boolean isStartup()@b@ {@b@ return startup;@b@ }@b@ @b@ /**@b@ * 启动客户端@b@ */@b@ public static void main(String[] args) @b@ {@b@ UploadClient.startup(PropertieFileReader.getString("uploadServerHost"),Integer.parseInt(PropertieFileReader.getString("uploadDataPort")));@b@ }@b@}
UploadServer.java
import java.io.BufferedReader;@b@import java.io.IOException;@b@import java.io.InputStreamReader;@b@import java.io.PrintWriter;@b@import java.net.ServerSocket;@b@import java.net.Socket;@b@@b@import com.xwood.ps.util.PropertieFileReader;@b@@b@@b@/**@b@ * 服务器端@b@ */@b@public class UploadServer extends Thread@b@{@b@ static boolean startup=true;@b@ static private int port;@b@ static private ServerSocket ss=null; @b@ private BufferedReader in;@b@ private PrintWriter out;@b@ private UploadServer(){}@b@ @b@ public void run() @b@ {@b@ try @b@ {@b@ while(startup)@b@ {@b@ Socket socket = ss.accept(); @b@ @b@ String remoteIP = socket.getInetAddress().getHostAddress();@b@ String remotePort = ":"+socket.getLocalPort();@b@ System.out.println("有客户端请求进来!IP:"+remoteIP+remotePort);@b@ out = new PrintWriter(socket.getOutputStream(),true);@b@ BufferedReader line = new BufferedReader(new InputStreamReader(System.in));@b@ out.println(line.readLine());@b@ //in = new BufferedReader(new InputStreamReader(socket.getInputStream()));@b@ // String line = in.readLine();@b@ // System.out.println("客户端发送内容:"+line);@b@ out.close();@b@ // in.close();@b@ socket.close();@b@ }@b@ }@b@ catch (IOException e) @b@ {@b@ // TODO Auto-generated catch block@b@ e.printStackTrace();@b@ }@b@ }@b@ @b@ /**@b@ * 开启服务器端@b@ * @param port1@b@ */@b@ public static void startup(int port1){ @b@ try {@b@ port=port1; @b@ startup=true;@b@ UploadServer server=new UploadServer();@b@ ss = new ServerSocket(port);@b@ server.start();@b@ System.out.println("socket端口"+port);@b@ } catch (IOException e) {@b@ System.out.println("服务端出现异常:"+e.toString());@b@ e.printStackTrace();@b@ }@b@ }@b@ @b@ /**@b@ * 关闭服务器端@b@ */@b@ public static void shutdown()@b@ {@b@ try @b@ {@b@ startup=false; @b@ ss.close();@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@ }@b@ @b@ /**@b@ * @return 启动状态@b@ */@b@ public static boolean isStartup()@b@ {@b@ return startup;@b@ }@b@ @b@ /**@b@ * 启动服务器端@b@ */@b@ public static void main(String[] args) throws Exception@b@ {@b@ System.out.println(Integer.parseInt(PropertieFileReader.getString("uploadDataPort")));@b@ UploadServer.startup(Integer.parseInt(PropertieFileReader.getString("uploadDataPort")));@b@ }@b@}
二、相关下载
1. jacob包&打印示例代码下载(示例代码如下)
import com.jacob.activeX.ActiveXComponent;@b@import com.jacob.com.ComThread;@b@import com.jacob.com.Dispatch;@b@import com.jacob.com.Variant; @b@@b@public class TestPrint@b@ {@b@ @b@ /**@b@ * 打印Excel文件@b@ * @b@ * @param path@b@ * Excel文件路径@b@ * @throws Exception @b@ */@b@ public static void printExcel(String path, String printerName) throws Exception {@b@ ComThread.InitSTA();@b@ ActiveXComponent xl = new ActiveXComponent("Excel.Application");@b@ try {@b@@b@ Dispatch workbooks = xl.getProperty("Workbooks").toDispatch();@b@@b@ Dispatch workbook = Dispatch.call(workbooks, "Open", path).toDispatch();@b@@b@ Dispatch.callN(workbook, "PrintOut", new Object[] { Variant.VT_MISSING, Variant.VT_MISSING, new Integer(1),@b@ new Boolean(false), printerName, new Boolean(true), Variant.VT_MISSING, "" });@b@ Dispatch.call(workbook, "Close");@b@@b@ } catch (Exception e) {@b@ e.printStackTrace();@b@ throw e;@b@ } finally {@b@ // 始终释放资源@b@ xl.invoke("Quit", new Variant[] {});@b@ ComThread.Release();@b@ }@b@ }@b@ @b@ public static void printTest()@b@ {@b@ try@b@ {@b@ ComThread.InitSTA();@b@ ActiveXComponent xl = new ActiveXComponent("Excel.Application"); @b@ //new Variant(false) 打印时是否显示文档 false不显示 @b@ Dispatch.put(xl, "Visible", new Variant(true)); @b@ Dispatch workbooks = xl.getProperty("Workbooks").toDispatch(); @b@ @b@ //打开文档 @b@ Dispatch excel=Dispatch.call(workbooks,"Open","D:/1_PO差异报表.xls").toDispatch(); @b@ @b@ Dispatch.get(excel,"PrintOut"); @b@ @b@@b@ }catch (Exception e)@b@ {@b@ e.printStackTrace();@b@ }finally@b@ { @b@ ComThread.Release();@b@ }@b@ @b@ }@b@ @b@ @b@ public static void main(String[] args)@b@ {@b@ try {@b@ TestPrint.printExcel("D:/1_PO差异报表.xls", "C4300-3F-right");@b@ } catch (Exception e) {@b@ // TODO Auto-generated catch block@b@ e.printStackTrace();@b@ }@b@ }@b@ }
2. jcom源码(C++)&jar包下载