用PrintServiceLookup.lookupPrintServices获取的服务,使用PrinterJob.setPrintService调用还会报错invalid name of printservice
时间: 2023-06-19 12:05:29 浏览: 123
这个错误通常是因为你传递给`setPrintService`方法的`PrintService`对象的名称是无效的。在调用`lookupPrintServices`方法获取打印服务的列表时,每个打印服务都有一个唯一的名称。你需要使用正确的名称来创建一个`PrintService`对象并将其传递给`setPrintService`方法。
你可以尝试使用以下代码来获取打印服务并设置打印服务:
```java
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
if (services.length > 0) {
PrintService printService = services[0]; // 获取第一个打印服务
PrinterJob printerJob = PrinterJob.getPrinterJob();
printerJob.setPrintService(printService); // 设置打印服务
} else {
System.out.println("No printers available.");
}
```
如果你仍然遇到问题,请检查打印服务的名称是否正确,并确保它与你传递给`setPrintService`方法的名称匹配。
相关问题
java调用打印机服务 多次后提示 invalid name of printservice
这个错误通常是由于打印机服务的名称无效或已经被关闭导致的。有几种可能的解决方法:
1. 检查打印机服务的名称是否正确。可以使用以下代码段来列出系统上可用的打印机服务名称:
```
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
for (PrintService service : services) {
System.out.println(service.getName());
}
```
确保打印机服务的名称与代码中使用的名称相同。
2. 确保打印机服务处于打开状态。可以尝试在代码中添加以下行,以确保打印机服务已打开:
```
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
if (!printService.isDocFlavorSupported(DocFlavor.SERVICE_FORMATTED.PRINTABLE)) {
throw new IllegalStateException("Invalid printer service name.");
}
```
3. 如果您已经多次调用打印机服务,并且出现无效名称错误,则可以尝试重新启动打印机服务。可以使用以下命令来实现:
```
sudo launchctl stop org.cups.cupsd
sudo launchctl start org.cups.cupsd
```
这将停止并重新启动打印机服务,可能会解决问题。
希望这些解决方法能够帮助您解决问题。
import javax.print.*; import javax.print.attribute.*; import javax.print.attribute.standard.*; public class HPPrinter { public static void main(String[] args) { // 获取系统中所有打印机的名称 PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null); for (PrintService service : services) { System.out.println(service.getName()); } // 选择要连接的打印机 String printerName = "Your Printer Name"; PrintService printerService = null; for (PrintService service : services) { if (service.getName().equals(printerName)) { printerService = service; break; } } if (printerService != null) { try { // 创建打印作业 DocPrintJob printJob = printerService.createPrintJob(); // 设置打印参数 PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet(); attributes.add(new Copies(1)); // 设置打印模式为彩色 String colorMode = "ON"; String command = "\u001B%-12345X@PJL SET COLOR=" + colorMode + "\r\n"; byte[] bytes = command.getBytes("ISO-8859-1"); DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE; Doc doc = new SimpleDoc(bytes, flavor, null); // 提交打印作业 printJob.print(doc, attributes); } catch (Exception e) { e.printStackTrace(); } } else { System.out.println("Printer not found."); } } } 这段代码添加获取文件并使用打印机可以打印
好的,可以在上面的代码中添加获取文件并使用打印机的功能。可以参考下面的代码:
```java
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;
import java.io.*;
public class HPPrinter {
public static void main(String[] args) {
// 获取系统中所有打印机的名称
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
for (PrintService service : services) {
System.out.println(service.getName());
}
// 选择要连接的打印机
String printerName = "Your Printer Name";
PrintService printerService = null;
for (PrintService service : services) {
if (service.getName().equals(printerName)) {
printerService = service;
break;
}
}
if (printerService != null) {
try {
// 获取要打印的文件
String filePath = "C:/test.pdf";
FileInputStream fileInputStream = new FileInputStream(filePath);
// 创建打印作业
DocPrintJob printJob = printerService.createPrintJob();
// 设置打印参数
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(new Copies(1));
// 设置打印模式为彩色
String colorMode = "ON";
String command = "\u001B%-12345X@PJL SET COLOR=" + colorMode + "\r\n";
byte[] bytes = command.getBytes("ISO-8859-1");
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
Doc doc = new SimpleDoc(bytes, flavor, null);
// 提交打印作业
Doc docToPrint = new SimpleDoc(fileInputStream, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
printJob.print(doc, attributes);
printJob.print(docToPrint, attributes);
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("Printer not found.");
}
}
}
```
其中,`FileInputStream`用来读取要打印的文件,`Doc`用来发送打印机命令,`DocFlavor.INPUT_STREAM.AUTOSENSE`用来告诉打印机要打印的是一个输入流。最后,将要打印的文件和发送打印机命令的`Doc`一起提交给打印作业即可。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)