Java中的打印机问题

| 我有一个问题,发生的事情是我要发送以在打印机上打印文件,为此我获得了已联网的打印机的IP地址,然后选择第一个,这是此代码:
PrintService[] service = PrinterJob.lookupPrintServices();// list of ip address 

PrinterJob printJob = PrinterJob.getPrinterJob();

printJob.setPrintService(service[0]);//I get the first address 
但是,现在我想分配一个包含IP地址的字符串:我想要的打印机的IP地址:ѭ1have,而不是我拥有的网络,并且那里不知道如何,有人请帮助我,我已经搜索过解决方案,但我没有取得好的结果。     
已邀请:
我猜想
PrintService
具有一定的性质,可为您提供路径。因此,遍历ѭ2array的数组以找到与您拥有的路径匹配的一个并使用它:
PrintService[] services = PrinterJob.lookupPrintServices();// list of ip address
String myPrinter = \"10.100.20.26\\My printer\";
PrintService serviceToUse = null;

for (PrintService service: services) {
    if (service.getPath().equals(myPrinter)) {
        serviceToUse = service;
        break;
    }
}

if (serviceToUse != null) {
    PrinterJob printJob = PrinterJob.getPrinterJob();

    printJob.setPrintService(serviceToUse);
}
    
 public void printFile(File file, String printerIp) throws PrintException, IOException {

        Socket socket = new Socket(printerIp, 9100);

        FileInputStream fileInputStream = new FileInputStream(file);
        byte [] mybytearray  = new byte [(int)file.length()];

        fileInputStream.read(mybytearray,0,mybytearray.length);

        OutputStream outputStream = socket.getOutputStream();

        outputStream.write(mybytearray,0,mybytearray.length);

             //Curious thing is that we have to wait some time to make more prints.
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {

        }

        outputStream.flush();
        outputStream.close();
        socket.close();
        fileInputStream.close();
    }

    //void main
      File f = new File(\"C:\\\\Users\\\\SHINWAR\\\\Desktop\\\\link.txt\");
    try {
        printFile(f, \"192.168.1.100\"); //f : file to print , ip printer
    } catch (Exception e) {
        System.out.println(e + \"--file\");
    }
从ip打印并发送文件.txt     

要回复问题请先登录注册