hz-zhhq-app-service/greenH5modul/.svn/pristine/2a/2a89f3866aab13f5b5a5de423ba...

94 lines
3.2 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.nationalelectirc.utils;
import java.awt.print.PrinterJob;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.OrientationRequested;
import javax.print.attribute.standard.Sides;
public class PrintUtil {
public static void main(String[] argv) throws Exception {
String filePath ="http://192.168.0.218:8888/sealFile/image/nWSImageDes/11-3e2bfdccefae446ebf95f8901deb1a76-WaterMark.png";
String printerName = "jysoft11111";//打印机名包含字串
URL url =new URL(filePath); // 创建URL
URLConnection urlconn = url.openConnection(); // 试图连接并取得返回状态码
urlconn.connect();
HttpURLConnection httpconn =(HttpURLConnection)urlconn;
if(httpconn.getResponseCode() != HttpURLConnection.HTTP_OK) {
System.out.print("无法连接到目标文件");
} else {
urlconn.getInputStream();
InputStream inputStream = urlconn.getInputStream();
JPGPrint(inputStream,printerName);
}
}
// 传入文件和打印机名称
public static void JPGPrint(InputStream fis,String printerName) throws PrintException {
try {
// 设置打印格式如果未确定类型可选择autosense
DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
// 设置打印参数
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(1)); //份数
aset.add(OrientationRequested.LANDSCAPE);
//aset.add(MediaSize.ISO.A4); //纸张
// aset.add(Finishings.STAPLE);//装订
aset.add(Sides.DUPLEX);//单双面
// 定位打印服务
PrintService printService = null;
if (printerName != null) {
//获得本台电脑连接的所有打印机
PrintService[] printServices = PrinterJob.lookupPrintServices();
if(printServices == null || printServices.length == 0) {
System.out.print("打印失败,未找到可用打印机,请检查。");
return ;
}
//匹配指定打印机
for (int i = 0;i < printServices.length; i++) {
System.out.println(printServices[i].getName());
if (printServices[i].getName().contains(printerName)) {
printService = printServices[i];
break;
}
}
if(printService==null){
System.out.print("打印失败,未找到名称为" + printerName + "的打印机,请检查。");
return ;
}
}
// fis = new FileInputStream(file); // 构造待打印的文件流
Doc doc = new SimpleDoc(fis, flavor, null);
DocPrintJob job = printService.createPrintJob(); // 创建打印作业
job.print(doc, aset);
} catch (Exception e1) {
e1.printStackTrace();
} finally {
// 关闭打印的文件流
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}