单位内网连接打印机

单位的网络属于专有的内网,用的不是WiFi,所以添加打印机时就搜索不到。

1.首先查看打印机的IP地址,每个打印机方式不一样。

2.在自己的网络中添加这个IP地址,注意:不能跟打印机地址一样,后面随便改一下。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要查询内网打印机的 IP 地址并连接它,可以使用以下代码: ```java import javax.print.PrintService; import javax.print.PrintServiceLookup; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.PrintRequestAttributeSet; import javax.print.attribute.standard.Copies; import javax.print.attribute.standard.PrinterState; import javax.print.attribute.standard.Sides; import java.net.InetAddress; import java.net.UnknownHostException; public class PrinterConnection { public static void main(String[] args) { String printerName = "Printer Name"; // 打印机名称 // 获取打印机列表 PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null); for (PrintService printService : printServices) { if (printService.getName().equals(printerName)) { // 获取打印机状态 PrinterState printerState = (PrinterState) printService.getAttribute(PrinterState.class); if (printerState != null) { System.out.println("Printer Status: " + printerState.toString()); } else { System.out.println("Cannot get printer status"); } // 获取打印机 IP 地址 String ipAddress = null; try { ipAddress = InetAddress.getByName(printService.getName()).getHostAddress(); } catch (UnknownHostException e) { e.printStackTrace(); } // 连接打印机 if (ipAddress != null) { String printerURI = "socket://" + ipAddress + ":9100"; // 打印机 URI PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet(); printRequestAttributeSet.add(new Copies(1)); // 打印份数 printRequestAttributeSet.add(Sides.DUPLEX); // 双面打印 // TODO: 添加需要打印的内容 // ... // 打印 try { printService.createPrintJob().print(printRequestAttributeSet); System.out.println("Printing..."); } catch (Exception e) { e.printStackTrace(); } } break; } } } } ``` 在上述代码中,我们首先获取系统中所有的打印机列表,然后遍历打印机列表,查找指定名称的打印机。一旦找到指定的打印机,我们使用 `getAttribute` 方法获取打印机状态属性,并将其转换为 `PrinterState` 对象。接着,我们使用 `InetAddress` 类获取打印机的 IP 地址。最后,我们使用打印机的 IP 地址连接打印机,并使用打印机的 URI 创建打印任务打印需要打印的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值