0

我正在开发系统,该系统经常打印报告,并且总是在检查打印机的状态(缺纸、缺纸等)。我已经实现了类,它查询打印机文件(/dev/usb/lp0)的状态,如打印机(Swecoin TTP2030)手册中所写,这是一个代码:

 public class PrinterStatusEnquier {
    private static Logger logger = Logger.getLogger(PrinterStatusEnquier.class);

    private static byte[] PAPER_NEAR_END_ENQUIRY = {0x1B, 0x05, 0x02}; 
    private static byte[] STATUS_ENQUIRY = {0x1B, 0x05, 0x01}; 

    private static String PRINTER_DEVICE = 
               PropertiesManager.getInstance().getApplicationProperty("printer.device.file");

    public static PaperStatus enquiryPaperStatus() throws IOException {
        logger.debug("In method enquiryPaperStatus()...");

        RandomAccessFile device = null;
        try
        {
            device = new RandomAccessFile(PRINTER_DEVICE, "rw");

            device.write(PAPER_NEAR_END_ENQUIRY);
            int response = device.readByte();

            return PaperStatus.getStatus(response);
        } catch (IOException e) {
            logger.error("Error while opening file: " + e.getMessage());
            e.printStackTrace();
            throw e;
        } finally {
            if (device != null) {
                logger.debug("Closing file...");
                device.close();
            }
        }
    }

    public static PrinterStatus enquiryPrinterStatus() throws IOException {
        logger.debug("In method enquiryPrinterStatus()...");

        RandomAccessFile device = null;
        try {
            device = new RandomAccessFile(PRINTER_DEVICE, "rw");

            device.write(STATUS_ENQUIRY);

            byte[] response = new byte[2];
            device.read(response);

            return PrinterStatus.getStatus(response);
        } catch (IOException e) {
            logger.error("Error while opening file: " + e.getMessage());
            e.printStackTrace();
            throw e;
        } finally {
            if (device != null) {
                logger.debug("Closing file...");
                device.close();
            }
        }       
    }

在我的系统之外,这段代码效果很好。但是当我将它集成到系统中时,它开始引发很多 IOExceptions。我注意到当打印机打印某些东西时会发生这种情况,此时我尝试获取状态。有时我会遇到异常(找不到文件),在这种情况下,我可以像这样检查打印机文件 -file.canWrite()但有时我会遇到异常(设备或资源忙)或(输入/输出错误),在这种情况下file.canWrite()不会帮助。最糟糕的是,打印机状态查询器不仅会抛出异常,还会锁定打印机文件一段时间。它仍然可以打印,但无法用于询问者。

是否存在在打印时查询打印机文件的方法?或者可能存在一种方法来检查打印机文件是否可用。请帮忙!

PS:系统是Ubuntu 12.10

Updae:我正在通过 DocPrintJob 对象打印:job.print();我还尝试打印调用 shell 命令:

Process p;
p = Runtime.getRuntime().exec("cat output.pdf | lpr");
p.waitFor();

但我面临同样的问题。

4

0 回答 0