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