在网络打印机上打印的 Java 应用程序 [英] Java application to print on Network printer

查看:77
本文介绍了在网络打印机上打印的 Java 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个示例应用程序以在网络打印机上打印文件.但我无法获得成功.伙计们请帮我解决这个问题.

I am working on a sample application to print file on network printer. But i m unable to get success. Guys please help me out to get rid this problem.

FileInputStream fis = new FileInputStream(file);
if (fis == null) {
System.out.print("No File");
return;
}
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();

PrintService service = PrintServiceLookup.lookupDefaultPrintService();

aset.add(new PrinterName("ipp:\\witnw21va\ipp\ITDepartment-HP4050", null));
//PrintServiceAttributeSet aset = HashPrintAttributeSet();
PrintService[] pservices = PrintServiceLookup.lookupPrintServices(flavor, aset);

if (service != null){
System.out.println("Default Printer: " + service.getName());

// Creating DocPrintJob
DocPrintJob job = service.createPrintJob();
try{
Doc doc = new SimpleDoc(fis,flavor,null);

PrintJobWatcher pjDone = new PrintJobWatcher(job);

job.print(doc,aset);

// Wait for the print job to be done
pjDone.waitForDone();

fis.close();
}

非常感谢.

推荐答案

该代码无法编译,因为您的 打印机名称中的转义序列:

That code won't compile, because you have invalid escape sequences in the printer name:

new PrinterName("ipp:\\witnw21va\ipp\ITDepartment-HP4050", null)

Java 编译器认为您正在尝试编写诸如换行符 \n 之类的特殊字符,并被 \w\i 等混淆在那个字符串中,这是不合法的.

The Java compiler thinks you are trying to write special characters like newline \n, and is confused by \w, \i etc in that string, which are not legal.

您需要对每个反斜杠进行转义以使其合法:

You need to escape each backslash to make it legal:

new PrinterName("ipp:\\\\witnw21va\\ipp\\ITDepartment-HP4050", null)

或者更改它,如果它实际上应该是正斜杠

or change it if it should in fact be forward slashes

这篇关于在网络打印机上打印的 Java 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆