2011-03-02 67 views
1

我在打印這臺打印機時遇到了問題。使用Java在Printronix T5000r上通過以太網打印

public void print(String fileName, String printerIp) { 

try { 
    BufferedReader streamIn = new BufferedReader(new FileReader(fileName)); 
    String line; 

    Socket socket = new Socket(printerIp, 9100); 
    Writer writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); 

    while ((line = streamIn.readLine()) != null) { 
    writer.write(line); 
    } 
    writer.flush(); 
    socket.close(); 
    streamIn.close(); 

} 

的情況看起來一切都很好,但是打印機不打印,當我使用其他程序打印一切工作正常。 有什麼想法?

+1

您是否使用了正確的協議?您在這裏看到的是您將文件直接轉儲到打印機 - 我認爲您需要使用適當的打印協議:http://en.wikipedia.org/wiki/JetDirect – Piskvor 2011-03-02 13:04:56

+0

此外,這可能是有趣的:http://download.oracle.com/javase/7/docs/technotes/guides/jps/spec/printing.fm6.html#1000147 – Piskvor 2011-03-02 13:12:47

+0

關於printService我認爲沒有辦法通過IP指定打印機。你有沒有把整個文件轉儲到打印機的例子? – 2011-03-02 13:20:16

回答

1

解決方法是將整個文件寫入打印機。

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(); 
     }