java 打印功能示例(服务器端、web端Applet实现)

服务器端:

package test.print;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  
  import javax.print.Doc;
  import javax.print.DocFlavor;
  import javax.print.PrintException;
  import javax.print.PrintService;
  import javax.print.PrintServiceLookup;
  import javax.print.ServiceUI;
  import javax.print.SimpleDoc;
  import javax.print.attribute.HashPrintRequestAttributeSet;
  import javax.print.attribute.PrintRequestAttributeSet;
  
  public class Print {
  	public static void main(String[] args) throws PrintException,
  			FileNotFoundException {
  		// get all printers
  		PrintService[] printers = PrintServiceLookup.lookupPrintServices(null,
  				null);
  		// get default printer
  		PrintService defaultPrinter = PrintServiceLookup
  				.lookupDefaultPrintService();
  		// the contentType which to be printed
  		DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
  		// print attrituters
  		PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet();
  		// get a printer
  		PrintService dps = ServiceUI.printDialog(null, 100, 100, printers,
  				defaultPrinter, null, attrs);
  		// new one document
  		Doc doc = new SimpleDoc(new FileInputStream(new File(
  				"C:\\Users\\Administrator\\Pictures\\我的图片\\computer.jpg")),
  				flavor, null);
  		// print
  		dps.createPrintJob().print(doc, attrs);
  	}
  }
  

客户端:

import java.applet.Applet;
 import java.net.MalformedURLException;
 import java.net.URL;
 
 import javax.print.Doc;
 import javax.print.DocFlavor;
 import javax.print.PrintException;
 import javax.print.PrintService;
 import javax.print.PrintServiceLookup;
 import javax.print.ServiceUI;
 import javax.print.SimpleDoc;
 import javax.print.attribute.HashPrintRequestAttributeSet;
 import javax.print.attribute.PrintRequestAttributeSet;
 
 public class Print extends Applet {
 	private static final long serialVersionUID = 1L;
 
 	public void init() {
 		// get all printers
 		PrintService[] printers = PrintServiceLookup.lookupPrintServices(null,
 				null);
 		// get default printer
 		PrintService defaultPrinter = PrintServiceLookup
 				.lookupDefaultPrintService();
 		// the contentType which to be printed
 		DocFlavor flavor = DocFlavor.URL.JPEG;
 		// print attrituters
 		PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet();
 		// get a printer
 		PrintService dps = ServiceUI.printDialog(null, 100, 100, printers,
 				defaultPrinter, null, attrs);
 		// new one document
 		try {
 			Doc doc = new SimpleDoc(new URL(
 					"http://localhost:8080/ncTest/computer.jpg"), flavor, null);
 			// print
 			dps.createPrintJob().print(doc, attrs);
 		} catch (PrintException e) {
 			e.printStackTrace();
 		} catch (MalformedURLException me) {
 			me.printStackTrace();
 		}
 	}
 }
 

主页:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=GB2312">
  <title>printTest</title>
  </head>
  <body>
  <applet code="Print.class" width=0 height=0 codebase="."
  	archive="print.jar"> </applet>
  </body>
  </html>

展开阅读全文

没有更多推荐了,返回首页