通过套接字发送可序列化的类

时间:2018-12-02 12:49:10

标签: java arraylist serialization

我正在尝试通过套接字将电子邮件的ArrayList发送到服务器,但是当我尝试这样做时,我得到了 NotSerializableException:javafx.beans.property.SimpleObjectProperty ,我在我需要将Serializable实施为may Email类的论坛:

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);

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

1 个答案:

答案 0 :(得分:1)

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

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