ObjectInputStream 中的 java.io 异常
我已通过 GPS 设备连接我的服务器。为了读取数据,我使用
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
产生以下异常的代码:
java.io.StreamCorruptedException: invalid stream header: 24312C38
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at com.tm4u.ServerSocketExample.main(ServerSocketExample.java:34)
I have connected my server by gps device. And for reading data I'm using the code
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
that produces the following exception:
java.io.StreamCorruptedException: invalid stream header: 24312C38
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:783)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:280)
at com.tm4u.ServerSocketExample.main(ServerSocketExample.java:34)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该异常表明您从套接字获取的数据流不是有效的对象流。对象流旨在读取由 Java 进程序列化的 Java 对象。如果您正在从 GPS 设备读取数据,我怀疑这就是设备发送的流类型。
The exception suggests that the data stream you're getting from the socket is not a valid object stream. Object streams are intended to read Java objects that were serialized by a Java process. If you're reading data from a GPS device, I doubt that's the type of stream the device is sending.
我确定您需要使用 DataInputStream 或 BufferedInputStream 与 InputStreamReader 来从 GPS 设备读取数据。
ObjectInputStream用于读取其他应用程序通过ObjectOutputStream发送的Java对象。这些是不太兼容的流。例如:JVM对象序列化协议必须相同。我怀疑 GPS 设备制造商设计该设备就是为了以这种方式使用。
I'm sure you need to use DataInputStream or BufferedInputStream with InputStreamReader to read data from GPS device.
ObjectInputStream is used read Java objects sent by other application through the ObjectOutputStream. These are less compatible streams. For example: JVM object serialisation protocol must be the same. I doubt that GPS device manufacturer designed this device to be used in this way.