这里我已经编码以获取设备列表,我将检查每个设备的状态
DocFlavor myFormat = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService[] services =PrintServiceLookup.lookupPrintServices(myFormat, aset);
System.out.println("The following printers are available");
for (int i=0;i<services.length;i++) {
PrintService printService = services[i];
PrintServiceAttributeSet printServiceAttributes = printService.getAttributes();
PrinterState printerState =
(PrinterState)printServiceAttributes.get(PrinterState.class);
if (printerState != null){
System.out.println(services[i].getName() + " is online");
} else {
System.out.println(services[i].getName() + " is offline");
}
}
但问题是每次我都得到“离线”状态,即使打印机是打开或关闭的
最佳答案
我最近在从 PrintService 获取另一个属性时遇到了同样的问题。
事实上,它总是返回 null,因为该方法从未在 Java 类中实现过,很多属性都是这种情况。
如果你真的想得到这些信息,你将不得不使用 windows Print Spooler DLL,或者,如果你的打印机是网络打印机,通过 SNMP 查询这些信息。
关于java - 打印机可用性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12871396/