gpt4 book ai didi

java - 通过 Socket 发送可序列化的类

转载 作者:行者123 更新时间:2023-12-02 10:31:48 26 4
gpt4 key购买 nike

我正在尝试通过 Socket 将电子邮件的 ArrayList 发送到服务器,但是当我尝试这样做时,我得到一个 NotSerializedException: javafx.beans.property.SimpleObjectProperty 我在论坛,我需要在可能的电子邮件类中实现可序列化,这是这样的:

public class Email implements Serializable {

private final IntegerProperty id = new SimpleIntegerProperty();

public final IntegerProperty IDProperty() {
return this.id;
}

public final Integer getID() {
return this.IDProperty().get();
}

public final void setID(final Integer id) {
this.IDProperty().set(id);
}

private final StringProperty mittente = new SimpleStringProperty();

public final StringProperty MittenteProperty() {
return this.mittente;
}

public final String getMittente() {
return this.MittenteProperty().get();
}

public final void setMittente(final String mittente) {
this.MittenteProperty().set(mittente);
}

private final StringProperty destinatario = new SimpleStringProperty();

public final StringProperty DestinatarioProperty() {
return this.destinatario;
}

public final String getDestinatario() {
return this.DestinatarioProperty().get();
}

public final void setDestinatario(final String destinatario) {
this.DestinatarioProperty().set(destinatario);
}

private final StringProperty oggetto = new SimpleStringProperty();

public final StringProperty OggettoProperty() {
return this.oggetto;
}

public final String getOggetto() {
return this.OggettoProperty().get();
}

public final void setOggetto(final String oggetto) {
this.OggettoProperty().set(oggetto);
}

private final StringProperty testo = new SimpleStringProperty();

public final StringProperty TestoProperty() {
return this.testo;
}

public final String getTesto() {
return this.TestoProperty().get();
}

public final void setTesto(final String testo) {
this.TestoProperty().set(testo);
}

private final ObjectProperty<Date> data = new SimpleObjectProperty<Date>();

public final ObjectProperty<Date> DataProperty() {
return this.data;
}

public final Date getData() {
return this.data.get();
}

public final void setData(final Date data) {
this.data.set(data);
}

public Email (int id, String mittente, String destinatario, String oggetto, String testo, Date data) {
setID(id);
setMittente(mittente);
setDestinatario(destinatario);
setOggetto(oggetto);
setTesto(testo);
setData(data);
}
}

这是我尝试发送的部分:

ObjectOutputStream objectOutput = new ObjectOutputStream(incoming.getOutputStream());
objectOutput.writeObject(arr);

但是什么都没有改变。我应该修改什么?

最佳答案

您应该在 Email 类中实现 writeObjectreadObject 方法,因为它需要一些特殊处理(它具有不可序列化的字段)。

此外,在 readObject 中,您需要一些工作来初始化 final 字段。

最后这两个方法应该如下所示:

private void writeObject(java.io.ObjectOutputStream out) throws IOException {
out.writeInt(getID());
out.writeUTF(getMittente());
out.writeUTF(getDestinatario());
out.writeUTF(getOggetto());
out.writeUTF(getTesto());
out.writeObject(getData());
}

private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {

try {

Field field = this.getClass().getDeclaredField("id");
field.setAccessible(true);
field.set(this, new SimpleIntegerProperty());

field = this.getClass().getDeclaredField("mittente");
field.setAccessible(true);
field.set(this, new SimpleStringProperty());

field = this.getClass().getDeclaredField("destinatario");
field.setAccessible(true);
field.set(this, new SimpleStringProperty());

field = this.getClass().getDeclaredField("oggetto");
field.setAccessible(true);
field.set(this, new SimpleStringProperty());

field = this.getClass().getDeclaredField("testo");
field.setAccessible(true);
field.set(this, new SimpleStringProperty());

field = this.getClass().getDeclaredField("data");
field.setAccessible(true);
field.set(this, new SimpleObjectProperty<Date>());

} catch (NoSuchFieldException | IllegalAccessException e) {
throw new IOException(e);
}

setID(in.readInt());
setMittente(in.readUTF());
setDestinatario(in.readUTF());
setOggetto(in.readUTF());
setTesto(in.readUTF());
setData((Date)in.readObject());
}

关于java - 通过 Socket 发送可序列化的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53580376/

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