首先,我知道有一些关于 StreamCorruptedExceptions 的问题,但几乎没有关于“无效类型代码:01”的问题。
我有一个本地客户端连接到本地服务器。客户端尝试向服务器发送一个可序列化的“Task_Data”对象。我用来发送的代码是(去除了杂乱):
oos = new ObjectOutputStream(clientSocket.getOutputStream());
Task_Data task = new Task_Data();
task.setCheckMale(true);
task.setCheckAdult(true);
task.setAdditionalInfo("testing");
task.setTakeNotes(true);
task.setTakePhoto(true);
oos.writeObject(task);
oos.flush();
现在在接收端(服务器)我有:
Task_Data task = (Task_Data) ois.readObject();
System.out.println(task.getAdditionalInfo());
Eclipse 抛出此错误:java.io.StreamCorruptedException: invalid type code: 01
奇怪的是,像 UTF 这样的“原始”类型的传输没有任何问题。
也许原因在于我的序列化,Task_Data 类是:
public class Task_Data implements Serializable {
private static final long serialVersionUID = -226273890693695870L;
private boolean checkMale = true;
private boolean checkAdult = true;
private boolean takePhoto = true;
private boolean takeNotes = true;
private String additionalInfo = null;
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
out.writeBoolean(checkMale);
out.writeBoolean(checkAdult);
out.writeBoolean(takePhoto);
out.writeBoolean(takeNotes);
out.writeUTF(additionalInfo);
}
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
checkMale = in.readBoolean();
checkAdult = in.readBoolean();
takePhoto = in.readBoolean();
takeNotes = in.readBoolean();
additionalInfo = in.readUTF();
}