如何在java中找到打印机及其打印机驱动程序的IP地址?

问题描述 投票:0回答:1
   PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);

    System.out.println("Available Printer Drivers:");
    for (PrintService service : printServices) {
        String printerName = service.getName();
        String driverClassName = (String) service.getAttribute(PrintServiceLookup.DRIVER);

        System.out.println("Printer Name: " + printerName);
        System.out.println("Driver Class Name: " + driverClassName);
        System.out.println("--------------------------------------");
    }
}

上面的代码将获取已安装的打印机驱动程序并在控制台中打印。

使用该打印机名称,我想找到 IP 地址,假设每台打印机都像笔记本电脑一样连接到 WiFi。

java zebra-printers printers
1个回答
0
投票

你可以尝试像他们这样的东西:

import java.net.InetAddress;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;

public class PrinterIPFinder {
    public static void main(String[] args) {
        PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);

        System.out.println("Available Printer Drivers:");
        for (PrintService service : printServices) {
            String printerName = service.getName();
            String driverClassName = (String) service.getAttribute(PrintServiceLookup.DRIVER);

            System.out.println("Printer Name: " + printerName);
            System.out.println("Driver Class Name: " + driverClassName);
            
            try {
                InetAddress[] addresses = InetAddress.getAllByName(printerName);
                for (InetAddress address : addresses) {
                    System.out.println("IP Address: " + address.getHostAddress());
                }
            } catch (Exception e) {
                System.out.println("Could not find IP address for printer: " + printerName);
            }

            System.out.println("--------------------------------------");
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.