gpt4 book ai didi

java - 无法在 java 中使用 ObjectInputStream 和 SealedObject 来使用 readObject()。获取运行时错误

转载 作者:行者123 更新时间:2023-12-01 18:17:59 25 4
gpt4 key购买 nike

运行时错误如下:

线程“main”java.lang.ClassCastException中出现异常:类消息无法转换为类javax.crypto.SealedObject(消息位于加载程序“app”的未命名模块中;javax.crypto.SealedObject位于模块中加载器“bootstrap”的 java.base)
在 Server.listStoreCheckDelete(Server.java:93)
在 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();
}}

最佳答案

Exception in thread "main" java.lang.ClassCastException: class Message cannot be cast to class javax.crypto.SealedObject

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

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

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

关于java - 无法在 java 中使用 ObjectInputStream 和 SealedObject 来使用 readObject()。获取运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60330267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com