问题描述
目前我正在尝试使用 RMI 来传输缓冲的图像数据。据我所知,缓冲图像是不可序列化的,所以我写了一个包装类来尝试传输它。
public class SrlzImg implements Serializable {
private BufferedImage img;
public BufferedImage getImage(){
return image;
}
@Serial
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ImageIO.write(image,"jpg",buffer);
out.writeInt(buffer.size());
buffer.writeTo(out);
}
@Serial
private void readObject(ObjectInputStream in) throws IOException,ClassNotFoundException {
in.defaultReadObject();
int size = in.readInt();
byte[] buffer = new byte[size];
in.readFully(buffer);
this.image = ImageIO.read(new ByteArrayInputStream(buffer));
}
}
这是我在 remoteImplementation 中传输它的方式
public class RemoteImplementation extends UnicastRemoteObject implements RemoteInterface {
private SrlzImg img = new SrlzImg(new BufferImage(...));
...
@Override
public SrlzImg getImg() throws RemoteException {
return img;
}
}
这就是客户端获取图像的方式
BufferedImage img = remoteInterface.getImg().getImage();
目前我找不到这有什么问题,但是当客户端尝试使用上面的代码获取缓冲图像时,我得到 NotSerializableException
。任何人都可以看到这里发生了什么?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)