有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java通过socket发送一个可序列化的类

我试图通过socket向服务器发送电子邮件的数组列表,但当我尝试发送时,我得到了一个NotSerializableException:javafx。豆。所有物SimpleObjectProperty我在论坛上读到,我需要在may Email类中实现Serializable,这是:

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) 个答案

  1. # 1 楼答案

    您应该在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());
    }