Java 如何使用(OCSF)将文件从客户端发送到服务器
他,, 我正在构建一个客户机-服务器项目,其中服务器可以同时连接到多个客户机。在我的项目中,我使用(OCSF)框架,使用handleMessageFromClient方法处理来自客户机的消息,并使用sendMessageToServer将消息发送到服务器。 如何将文件从客户端发送到服务器并将其保存在服务器中 这是服务器端的代码:Java 如何使用(OCSF)将文件从客户端发送到服务器,java,client-server,Java,Client Server,他,, 我正在构建一个客户机-服务器项目,其中服务器可以同时连接到多个客户机。在我的项目中,我使用(OCSF)框架,使用handleMessageFromClient方法处理来自客户机的消息,并使用sendMessageToServer将消息发送到服务器。 如何将文件从客户端发送到服务器并将其保存在服务器中 这是服务器端的代码: public void handleMessageFromClient(Object msg, ConnectionToClient client){ if(
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);
}
}
}
如何修改代码以发送和接收文件
我在github上有服务器和客户端代码:
我能够通过将文件转换为字节数组并将其作为对象发送到服务器来解决这个问题
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();}