1

我有一个问题,我想发送以在打印机上打印文件,为此我获取已联网的打印机的 IP 地址并选择第一个,这是代码:

PrintService[] service = PrinterJob.lookupPrintServices();// list of ip address 

PrinterJob printJob = PrinterJob.getPrinterJob();

printJob.setPrintService(service[0]);//I get the first address 

但现在我想分配包含 IP 地址的字符串:\\10.100.20.26\My printer我想要的打印机,而不是我拥有的网络,它在那里不知道如何,请有人帮助我,我已经搜索过解决方案,但我没有得到很好的结果。

4

2 回答 2

1

我猜它PrintService有一些属性可以给你它的路径。因此,请遍历PrintServices 数组以找到与您拥有的路径匹配的一个并使用它:

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);
}
于 2011-05-26T19:33:36.330 回答
0
 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

于 2018-07-26T12:27:24.333 回答