获取运行时错误如下:

Exception in thread "main" java.lang.ClassCastException: class Message cannot be cast to class javax.crypto.SealedObject (Message is in unnamed module of loader 'app'; javax.crypto.SealedObject is in module java.base of loader 'bootstrap') at Server.listStoreCheckDelete(Server.java:93) at Server.main(Server.java:69)

import java.io.*;
import java.net.*;
import java.util.*;
import java.security.*;
import javax.crypto.*;

class Message implements Serializable {
public String recipientHash;
        public Date timestamp;
        public byte[] key;
        public byte[] iv;
        public byte[] encryptedMsg;
        public byte[] signature;
}

class Server{
    static Map<String, String> map;
    static List<String> list;
    static String currentUserid;

    public static void main(String [] args) throws Exception {
        int port = Integer.parseInt(args[0]);
        ServerSocket ss = new ServerSocket(port);
        map = new HashMap<>();
        list=new ArrayList<String>();
        while(true)
        {
        int firstexe=1;
        System.out.println("Waiting rebellions connection...");
        Socket s = ss.accept();
        DataInputStream dis = new DataInputStream(s.getInputStream());
        DataOutputStream dos = new DataOutputStream(s.getOutputStream());
        String  rebellionMsg=null;
        Server.currentUserid=null;
        int numberofmsg=0;
        try {
            while ((rebellionMsg = dis.readUTF()) != null) {
                System.out.println(rebellionMsg);
                if (firstexe==1)
                        {
                firstexe=0;
                dos.writeUTF("Hey! You have "+numberofmsg+" for you!");
                dos.flush();
                }
                listStoreCheckDelete(rebellionMsg,s);
            }
        }
        catch(IOException e) {
            System.err.println("Client closed its connection. -->"+e);
        }
        }
    }

    public static void listStoreCheckDelete(String rebellionMsg,Socket s) throws Exception
    {
            String listAddition=null;
            ObjectInputStream in = new ObjectInputStream(s.getInputStream());
            SealedObject so = (SealedObject)in.readObject();
}}

最佳答案

线程“主”中的异常java.lang.ClassCastException:类消息
无法转换为类javax.crypto.SealedObject


        SealedObject so = (SealedObject)in.readObject();


已向您发送了一个Message类型而不是SealedObject类型的序列化对象。该行应显示为:

        Message message = (Message)in.readObject();

10-07 12:10