2017-06-02 44 views
0

他, 我正在構建一個客戶端服務器項目,其中服務器可以同時連接到多個客戶端,在我的項目中我正在使用(OCSF)框架,並且我使用handleMessageFromClient方法來處理來自客戶端的消息,並使用sendMessageToServer發送消息以斷開連接。 如何從客戶端發送文件到服務器並將文件保存在服務器中。如何使用(OCSF)發送文件從客戶端到服務器

這是服務器端的代碼:

public void handleMessageFromClient(Object msg, ConnectionToClient client){ 

    if(msg instanceof File){ 
     File file = (File)msg; 

    }else if(msg instanceof HashMap<?, ?>){ 
     @SuppressWarnings("unchecked") 
     HashMap<String, String> clientMsg = (HashMap<String, String>) msg; 

     // shows the received msg to the event log 
     logController.showMsg("Message received: " + clientMsg.get("msgType") + " from " + client); 


     //check the msg type 
     if(clientMsg.get("msgType").equals("Login")){ 
      login(clientMsg,client); 
     }else if(clientMsg.get("msgType").equals("select")){ 
      selectQuery(clientMsg, client); 
     }else if(clientMsg.get("msgType").equals("update")){ 
      updateQuery(clientMsg, client); 
     }else if(clientMsg.get("msgType").equals("delete")){ 
      updateQuery(clientMsg, client); 
     }else if(clientMsg.get("msgType").equals("insert")){ 
      updateQuery(clientMsg, client); 
     } 
    } 
} 

客戶

final public void sendToServer(Object msg) throws IOException{ 
if (clientSocket == null || output == null) 
     throw new SocketException("socket does not exist"); 

output.writeObject(msg); 
output.reset();} 

我如何可以修改代碼來發送和recive文件?

我有在github服務器和客戶端的代碼: client code sever code

回答

0

我能夠通過將文件轉換爲字節數組,將其發送到服務器作爲一個對象來解決它。

相關問題