我正在 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();
}