繁体   English   中英

如何使用(OCSF)从客户端向服务器发送文件

[英]How to send file from client to server using (OCSF)

他,我正在建立一个客户端服务器项目,在该项目中,服务器可以立即连接到多个客户端,在我的项目中,我正在使用(OCSF)框架,并且我正在使用handleMessageFromClient方法来处理来自客户端的消息,并使用sendMessageToServer将消息发送到服务器。 如何将文件从客户端发送到服务器并将文件保存在服务器中。

这是服务器端的代码:

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

我该如何修改发送和接收文件的代码?

我在github上有服务器和客户端代码: 客户端代码 服务器代码

我可以通过将文件转换为字节数组并将其作为对象发送到服务器来解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM