我的項目是編寫一個服務器和客戶端類。服務器等待接收文件的路徑,該文件將從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);
}
很多代碼。你說你達到了90%,如果你在執行代碼的時候調試/執行你的代碼,它會出錯哪裏?如果只是在最後的文件寫入,那麼這是一個微不足道的問題。 – mattias 2014-11-08 23:26:31
當我調試客戶端時,它根本沒有做任何事情。如果我在處理輸入和輸出流的for循環之前放置一箇中斷點,那麼它只是立即跳到循環並停在斷點處...... – 2014-11-09 09:23:19
它應該停在斷點處,這就是爲什麼它被稱爲中斷點。之後,你自己需要讓程序繼續流動。例如在Eclipse中,通常可以通過按鈕F5-F8完成不同的操作。 – mattias 2014-11-09 09:28:13