我在相同的端口号上添加了两台打印机(使用Ricoh通用打印机驱动程序),它运行正常。我编写了一个java程序,从套接字读取流并写入postscript文件。我正在运行两台打印机在同一个端口上,我可以通过两台打印机打印文件。但我的问题是,我怎么知道打印/流来自哪台打印机?
我知道我们可以获得默认的打印机名称,如下所示 PrintServiceLookup.lookupDefaultPrintService()。
答案 0 :(得分:0)
import javax.print.*;
class Test {
public static void main (String [] args)
{
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
System.out.println("Number of print services: " + printServices.length);
for (PrintService printer : printServices)
System.out.println("Printer: " + printer.getName());
}
} 使用上面的代码获取打印机列表然后检查每台打印机的状态,如果它在工作中忙,那么你的流来自那台打印机
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
AttributeSet attributes = printService.getAttributes();
String printerState = attributes.get(PrinterState.class).toString();
String printerStateReason = attributes.get(PrinterStateReason.class).toString();
System.out.println("printerState = " + printerState);
if(printerState.equals(javax.print.attribute.standard.PrinterState.PROCESSING)){
//this is printer from where stream is coming
}