2012-10-13 23 views
2

這裏我有編碼,以獲得設備列表,我會檢查每個設備的狀態打印機有效性的影響

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"); 
    } 
} 

但每一次我得到的狀態爲「脫機」,即使該打印機接通問題或關閉

+0

你確定printerState在它離線的情況下將爲空嗎? – banjara

+0

是的,當打印機打開和關閉時它會返回null – Bucks

回答

1

我最近有同樣的問題從PrintService獲取另一個屬性。

實際上,它總是返回null,因爲該方法從來沒有在Java類中實現過,並且這是很多屬性的情況。

如果您確實想獲得這些信息,您將不得不使用Windows Print Spooler DLL,或者如果您的打印機是網絡打印機,請通過SNMP查詢這些信息。