问题遇到的现象和发生背景
package com.company;
import javax.print.*;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.Size2DSyntax;
import javax.print.attribute.standard.*;
import javax.print.event.PrintJobEvent;
import javax.print.event.PrintJobListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
public class Main {
private static long j = 1;
/**
* 图片路径
* @param picPath
*/
public static void printer(String picPath) {
// 构建打印请求属性集
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
// 设置打印格式
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
// 查找所有的打印服务
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(flavor, pras);
// 这里直接用第一个
PrintService printService = printServices[0];
String printName = printService.getName();
// 判断当前打印机状态
// Winspool.PRINTER_INFO_2 printerInfo2 = WinspoolUtil.getPrinterInfo2(printName);
// if (printerInfo2.Status != 0) {
// return;
// }
File picFile = new File(picPath);
if (!picFile.exists() || !picFile.isFile()) {
return ;
}
PrintService printer = PrintServiceLookup.lookupDefaultPrintService();
Media[] objs = (Media[]) printer.getSupportedAttributeValues(Media.class, null, null);
for (Media obj : objs) {
if (obj instanceof MediaSizeName) {
System.out.println("纸张型号:" + obj);
} else if (obj instanceof MediaTray) {
System.out.println("纸张来源:" + obj.getName());
}
}
String fileName = picFile.getName();
// 获取图片后缀名,设置打印文件格式
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
switch (suffix) {
case "jpg":
case "jpeg":
flavor = DocFlavor.INPUT_STREAM.JPEG;
break;
case "png":
flavor = DocFlavor.INPUT_STREAM.PNG;
break;
case "pdf":
flavor = DocFlavor.INPUT_STREAM.PDF;
break;
case "gif":
flavor = DocFlavor.INPUT_STREAM.GIF;
default:
break;
}
// 设置打印文件名
pras.add(new JobName(fileName, Locale.CHINA));
// 设置打印方向
pras.add(OrientationRequested.LANDSCAPE);
// 设置打印区域
MediaPrintableArea mp = new MediaPrintableArea(0, 0, 500 , 900, Size2DSyntax.MM);
pras.add(mp);
try {
FileInputStream fis = new FileInputStream(picFile);
HashDocAttributeSet das = new HashDocAttributeSet();
for (long i = 0; i < j; i++) {
// 使用打印服务生成打印任务
DocPrintJob job = printService.createPrintJob();
// 获取需要打印的文档类doc
SimpleDoc doc = new SimpleDoc(fis, flavor, das);
// 监听打印任务
job.addPrintJobListener(new PrintJobListener() {
@Override
public void printDataTransferCompleted(PrintJobEvent pje) {
System.out.println("成功发送任务:" + fileName);
}
@Override
public void printJobCompleted(PrintJobEvent pje) {
System.out.println("打印任务完成!");
}
@Override
public void printJobFailed(PrintJobEvent pje) {
System.out.println("打印任务失败:" + fileName);
}
@Override
public void printJobCanceled(PrintJobEvent pje) {
System.out.println("取消打印任务!");
}
@Override
public void printJobNoMoreEvents(PrintJobEvent pje) {
System.out.println("没有更多方法提供打印机调用!");
}
@Override
public void printJobRequiresAttention(PrintJobEvent pje) {
System.out.println("纸张已用完");
}
});
job.print(doc, pras);
}
} catch (PrintException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
File file = new File("pic");
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
String url = "pic/"+files[i].getName();
System.out.println(url);
printer(url);
TimeUnit.SECONDS.sleep(4);//秒
}
}
}
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果