简体   繁体   English

通过Java中的IP地址查找并打印到网络打印机

[英]Find and print to network printer by IP address in Java

I've read tons of similar questions and haven't gotten anywhere.. 我读过很多类似的问题,而且一无所获。

What I'm trying to do is connect to a printer directly by IP (not one that's in my windows devices list) and print a document to it. 我想做的是通过IP直接连接到打印机(不是Windows设备列表中的一个),然后向其中打印文档。

Here's what I've tried so far (didn't work, no idea why): 这是到目前为止我尝试过的方法(没有用,不知道为什么):

try{
    Socket sock = new Socket("1.2.3.4", 9100);
    PrintWriter oStream = new PrintWriter(sock.getOutputStream());
        oStream.println("Hello World");
        oStream.close();
        sock.close(); 
}
catch (UnknownHostException e){
    e.printStackTrace();
} 
catch (IOException e) { 
    e.printStackTrace();
}

I found this for your problem. 我发现这是您的问题。 You can try your network printer with this code. 您可以尝试使用此代码的网络打印机。

https://blog.entelect.co.za/view/3213/android-printing-to-a-network-printer https://blog.entelect.co.za/view/3213/android-printing-to-a-network-printer

I also saw many examples just like your example worked. 我也看到了许多示例,就像您的示例正常工作一样。 So I guess there could be a connection problem to printer. 因此,我认为打印机可能存在连接问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM