2014-11-08 61 views
0

我的項目是編寫一個服務器和客戶端類。服務器等待接收文件的路徑,該文件將從CSV轉換爲XML,然後應該將文件的內容讀回到將內容保存到新文件的客戶端。 到目前爲止,我已經完成了90%的工作,唯一的問題是文件最後是空的,但是在服務器端創建了XML文件。將文件讀入字節數組並將其傳輸到一個新文件中

這是服務器連接處理方法

public void handleConnection(InputStream sockInput, OutputStream sockOutput){ 
    CsvToXml csv = new CsvToXml(); 
    String csvAddress; 

    while(true) 
    { 
     byte[] buffer = new byte[4094]; 
     int bytes_read = 0; 
     byte[] bytes_out = new byte[4094]; 

     try { 
      bytes_read = sockInput.read(buffer, 0, buffer.length); 
      csvAddress = new String(buffer, 0, bytes_read); 
      System.err.println(csvAddress); 
      csv.convertFile(csvAddress , "C:/Users/Arian/Desktop/xmlFile.xml", ","); 

      if(bytes_read < 0){ 
       System.err.println("Tried to read from socket, but returned < 0. Closing socket."); 
       return; 
      } 


      System.out.println("Server Received "+ bytes_read + "bytes, data=" + (new String(buffer, 0, bytes_read))); 


      bytes_out = readXml(); 
      bytes_read = sockInput.read(bytes_out, 0, bytes_out.length); 
      sockOutput.write(bytes_out); 

      sockOutput.flush(); 
     } catch(Exception e){ 
      System.err.println("Exception reading/writing from/to socket, e=" +e); 
      e.printStackTrace(System.err); 
      return; 
     } 
    } 
} 

所以這部分我已閱讀並把XML文件到字節數組並返回。

public byte[] readXml(){ 

    File file = new File("C:/Users/Arian/Desktop/xmlFile.xml"); 
    byte[] xmlFile = new byte[(int)file.length()]; 
    FileInputStream fileInputStream = null; 


    try { 
     fileInputStream = new FileInputStream(file); 
     fileInputStream.read(xmlFile); 
     fileInputStream.close(); 
    } catch (Exception e) { 
     System.err.println("File not found or unreadable."); 
     e.printStackTrace(); 
    } 

    return xmlFile; 
} 

,這是 {

System.err.println("Opening connection to "+serverHostname+" port "+serverPort); 
    try { 
     sock = new Socket(serverHostname, serverPort); 
     sockInput = sock.getInputStream(); 
     sockOutput = sock.getOutputStream(); 
    } catch (IOException e) { 
     e.printStackTrace(System.err); 
     return; 
    } 

    System.err.println("About to start reading/rwriting to/from socket"); 

    for(int i = 1; i <= iterations; i++){ 
     try{ 
      sockOutput.write(data, 0, data.length); 
      File file = new File("C:/Users/Arian/Desktop/NewFile.xml"); 
      byte[] buffer = new byte[sockInput.available()]; 
      sockOutput = new FileOutputStream(file); 
      int len; 
      sockInput.read(buffer); 
      sockOutput.write(buffer); 
      sockOutput.flush(); 


     } catch(IOException e){ 
      e.printStackTrace(System.err); 
     } 






    System.err.println("Done reading/writing to/from socket, closing socket."); 

    try { 
     sock.close(); 
    } catch (IOException e){ 
     System.err.println("Exception closing socket."); 
     e.printStackTrace(System.err); 
    } 

    System.err.println("Exiting."); 
    } 



} 

我試過很多方法的客戶InputStream和OutputStream方法

公共無效startConversion(INT迭代)我已經查看過的其他職位並沒有取得任何成功。我不能發佈兩個以上的鏈接,所以繼承我的服務器和客戶端類,我的轉換類無論如何都很好。

http://pastebin.com/C34hVNEW - 客戶端類http://pastebin.com/CF6BJTMP - 服務器類

UPDATE:

我改變了對循環我的客戶類中,以這一點,它仍然無法正常工作。我在客戶端停止調試時發表評論。

for(int i = 1; i <= iterations; i++){ 
     try{ 
      sockOutput.write(data, 0, data.length); 
      File file = new File("C:/Users/Arian/Desktop/NewFile.xml"); 
      byte[] buffer = new byte[8192]; 
      sockOutput = new FileOutputStream(file); 
      int count; 
      while((count = sockInput.read(buffer)) > 0){ //The client stops debugging here. 
       sockOutput.write(buffer, 0, count); 
      } 
      sockOutput.flush(); 


     } catch(IOException e){ 
      e.printStackTrace(System.err); 
     } 
+0

很多代碼。你說你達到了90%,如果你在執行代碼的時候調試/執行你的代碼,它會出錯哪裏?如果只是在最後的文件寫入,那麼這是一個微不足道的問題。 – mattias 2014-11-08 23:26:31

+0

當我調試客戶端時,它根本沒有做任何事情。如果我在處理輸入和輸出流的for循環之前放置一箇中斷點,那麼它只是立即跳到循環並停在斷點處...... – 2014-11-09 09:23:19

+0

它應該停在斷點處,這就是爲什麼它被稱爲中斷點。之後,你自己需要讓程序繼續流動。例如在Eclipse中,通常可以通過按鈕F5-F8完成不同的操作。 – mattias 2014-11-09 09:28:13

回答

1

你正在爲此做出一頓真正的美食。在Java流之間複製流的標準方法如下:

int count; 
byte[] buffer = new byte[8192]; // or whatever you like, any size greater than zero 
while ((count = in.read(buffer)) > 0) 
{ 
    out.write(buffer, 0, count); 
} 

請注意,您必須循環;您不需要輸入大小的緩衝區;並且在使用時必須使用讀取計數,因爲read()沒有義務填充緩衝區,並且至少在EOS之前的最後讀取中幾乎肯定不會。您可以在兩端使用相同的代碼,適當調整inout

+0

所以我改變了你提到的那一點,我實際上以前也是這樣設置的,所以我有隨機的「int len」。我有這樣的代碼,而不是幾乎相同的代碼,它仍然無法工作。我在調試中寫了一個更新錯誤的地方。 – 2014-11-09 09:27:55

相關問題