我在执行打印机程序时遇到异常。是否有任何其他方法可以指定未在系统中配置的网络打印机。
线程“main”中的异常java.lang.ClassCastException at javax.print.attribute.AttributeSetUtilities.verifyAttributeValue(AttributeSetUtilities.java:552) 在 javax.print.attribute.HashAttributeSet.add(HashAttributeSet.java:304) 在com.printing.PrintingService.main(PrintingService.java:118)
代码
public static void main(String [] a) throws FileNotFoundException, PrintException {
String text = " My first Printer Program";
InputStream fis = new ByteArrayInputStream(text.getBytes());
if (fis == null)
{
System.out.print("No File");
return;
}
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService[] service = PrintServiceLookup.lookupPrintServices(flavor, aset);
aset.add(new PrinterName("ipp://abcd.com/ipp/ITDepartment-HP4050", null)); ///// throwing exception on this line :ClassCastException
if (service != null){
System.out.println("Printer: " + service[0].getName());
DocPrintJob job = service[0].createPrintJob();
try{
Doc doc = new SimpleDoc(fis,flavor,null);
job.print(doc,aset);
fis.close();
}
catch(Exception e) {
System.out.println(e);
}
}
}