3

我正在java上编写一个 lpr 客户端,以将文件(PDF、doc 等)发送到打印机(hp laser-jet 4250dtn)客户端和服务器都在运行UNIX

打印机在服务器上(这就是我使用 lpr 的原因)一切都很好,我确实设法打开套接字,发送控制文件(根据 RFC 1179)并发送文件本身。

该文件正在进入打印机“队列”。

我的问题是当我想从打印机打印文件时,它出现错误并且根本无法理解(看不到任何文本,只是奇怪的符号)。

我的问题是我给出或制作什么文件类型(将文件更改为 postscript??)以便打印机能够理解我?还是我做错了什么?

我在此处附加代码,以便您可以查看并突出显示我从文件路径打开文件的行

Socket socketLpr = getSocket(hostName);
    socketLpr.setSoTimeout(30000);
    OutputStream sOut = socketLpr.getOutputStream();
    InputStream sIn = socketLpr.getInputStream();

    //Open printer
    s = "\002" + printerName + "\n";
    sOut.write(s.getBytes());
    sOut.flush();
    acknowledge(sIn, "lpr Failed to open printer");

    //Send control file     
    controlFile += "H" + hostName + "\n";
    controlFile += "P" + userName + "\n";
    controlFile += ((printRaw) ? "o":"p") +"dfA" + strJobNumber + hostName + "\n";
    controlFile += "UdfA" + strJobNumber + hostName + "\n";
    controlFile += "N" + documentName + "\n";

    s = "\002" + (controlFile.length()) + " cfA" + strJobNumber + hostName + "\n";
    sOut.write(s.getBytes());

    acknowledge(sIn, "lpr Failed to send control header");

    buffer = controlFile.getBytes();
    sOut.write(buffer);
    buffer[0] = 0;
    sOut.write(buffer, 0, 1);
    sOut.flush();

    acknowledge(sIn, "jLpr Failed to send control file");

    //Send print file       
    **f = new File(fileName);** // <- here is when im oppening the file(pdf,word,etc)
    if (!(f.exists() && f.isFile() && f.canRead())) {
        throw new IOException("jLpr Error opening print file");
    }
    s = "\003" + (f.length()) + " dfA" + strJobNumber + hostName + "\n";
    sOut.write(s.getBytes());
    sOut.flush();
    acknowledge(sIn, "jLpr Failed to send print file command");

    FileInputStream fs = new FileInputStream(f);

    int readCounter;
    do {
        readCounter = fs.read(buffer);
        if (readCounter>0) {
            sOut.write(buffer, 0, readCounter);
        }
    } while (readCounter>0);
    buffer[0] = 0;
    sOut.write(buffer,0,1);
    sOut.flush();
    acknowledge(sIn, "jLpr Failed to send print file");

    socketLpr.close();
}
4

0 回答 0