0

得到运行时错误如下:

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

1 回答 1

0

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

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

您已收到类型为序列化的对象Message,而不是SealedObject。该行应为:

        Message message = (Message)in.readObject();
于 2020-02-21T00:18:46.183 回答