Java 通过套接字发送可序列化类
我试图通过套接字向服务器发送电子邮件的ArrayList,但当我尝试发送时,我得到了一个NotSerializableException:javafx.beans.property.SimpleObjectProperty我在论坛上读到我需要将Serializable实现到may Email类中,这是:Java 通过套接字发送可序列化类,java,arraylist,serialization,Java,Arraylist,Serialization,我试图通过套接字向服务器发送电子邮件的ArrayList,但当我尝试发送时,我得到了一个NotSerializableException:javafx.beans.property.SimpleObjectProperty我在论坛上读到我需要将Serializable实现到may Email类中,这是: public class Email implements Serializable { private final IntegerProperty id = new SimpleIn
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);
}
}
但一切都没有改变。我应该修改什么?您应该在
Email
类中实现writeObject
和readObject
方法,因为它需要一些特殊处理(它有不可序列化的字段)
另外,在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());
}
private void writeObject(java.io.ObjectOutputStream out)抛出IOException{
out.writeInt(getID());
out.writeUTF(getMittente());
writeUTF(getDestinatario());
writeUTF(getOggetto());
writeUTF(getesto());
writeObject(getData());
}
私有void readObject(java.io.ObjectInputStream in)引发IOException、ClassNotFoundException{
试一试{
Field=this.getClass().getDeclaredField(“id”);
字段。setAccessible(true);
set(这是新的SimpleIntegerProperty());
field=this.getClass().getDeclaredField(“mittente”);
字段。setAccessible(true);
set(这是新的SimpleStringProperty());
field=this.getClass().getDeclaredField(“destinatario”);
字段。setAccessible(true);
set(这是新的SimpleStringProperty());
field=this.getClass().getDeclaredField(“oggetto”);
字段。setAccessible(true);
set(这是新的SimpleStringProperty());
field=this.getClass().getDeclaredField(“testo”);
字段。setAccessible(true);
set(这是新的SimpleStringProperty());
field=this.getClass().getDeclaredField(“数据”);
字段。setAccessible(true);
set(这是新的SimpleObjectProperty());
}捕获(NoSuchFieldException | IllegalacessException e){
抛出新的IOException(e);
}
setID(in.readInt());
setMittente(in.readUTF());
setDestinatario(in.readUTF());
setOggetto(in.readUTF());
setTesto(in.readUTF());
setData(.readObject()中的(日期);
}
由于类中的某些内容本身不可序列化(SimpleObjectProperty),并且您无法使其序列化,因此您需要做的不仅仅是添加可序列化的实现。你需要实际编写序列化和反序列化方法。我想知道这是否可能是一种伪装。您是否正在尝试序列化GUI组件(视图部分)?或者您正在尝试序列化程序的底层数据(模型部分)?还有,为什么要使用序列化来传输数据,而不是使用更友好的格式呢?@HovercraftFullOfEels我正在尝试序列化模型部分。这是必要的,因为如果我尝试发送电子邮件数组列表,我会得到我在问题中写的错误。@毫无价值,那么我应该在电子邮件列表中写两个方法吗?一个用于发送数据,另一个用于接收数据?非常感谢,它解决了写入错误!现在我得到了java.io.StreamCorruptedException:编译时出现意外的块数据错误。问题出在服务器中的这一行:ObjectInputStream inStream=newobjectinputstream(s.getInputStream());email=(ArrayList)inStream.readObject()代码>如何使用我在Email类中插入的新方法编写它?
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());
}