Java的StreamCorruptedException超过一定尺寸发送包含字节数据对象时 [英] Java StreamCorruptedException when sending object containing byte data over a certain size
问题描述
我有一个Java客户端 - 服务器(使用ocsf如果有人知道这里IT)基础架构,我用上传文件从客户端到服务器。客户实际上是一个Android应用程序(不知道的事项,许多与否在这种情况下)
我通过读取文件中的数据(字节),在包含其他一些细节的对象包装它(用户ID等),并在的ObjectOutputStream $ C发送这个对象这样做$ C>服务器。
似乎一切正常,直到该文件的字节数组超过一定规模(不知道这个奇怪的门槛是什么还没有,但它似乎645KB已经太多了)。然后,服务器抛出一个 StreamCorruptedException
试图读取从的ObjectInputStream
和关闭套接字对象时。
包含文件的字节对象信息的code:
公共类MessageUploadFile扩展MessageToServer {私有静态最后的serialVersionUID长= 2356276507283427913L;
私人字符串_destId;
私人TransferDetails _TD;
私人字节[] _fileData;
公共MessageUploadFile(字符串srcId,TransferDetails TD,字节[] FILEDATA){
超(srcId);
_destId = td.getDestinationId();
_TD =运输署;
_fileData = FILEDATA;}
客户端套接字和数据流初始化:
ClientSocket的=新的Socket(主机,端口);
输出=新的ObjectOutputStream(clientSocket.getOutputStream());
输入=新的ObjectInputStream(clientSocket.getInputStream());
使用发送邮件:
output.writeObject(MSG);
这些都是在服务器端的数据流初始化:
输入=新的ObjectInputStream(clientSocket.getInputStream());
输出=新的ObjectOutputStream(clientSocket.getOutputStream());
阅读使用消息:
味精= input.readObject();
StreamCorruptedException
一个Java StreamCorruptedException可以在deserialising数据被抛出。它在本质上发生在两种主要情况之一:
您尝试打开周围实际上并没有使用ObjectOutputStream写入一些数据的ObjectInputStream的
或
在一次的readObject()操作,流得在错误的地方。
从Java文档:
当从对象流中读取的控制信息违反内部一致性检查时抛出。
但我得到这个例外有大消息,搬到字节数组的解决方案。
有一个看看这篇文章: http://www.javamex.com/tutorials /io/StreamCorruptedException.shtml
总之,转换对象,并从字节数组,并重新创建它。
I have a Java client-server (using ocsf if anyone here knows it) infrastructure I am using to upload files from client to server. The client is actually an Android app (not sure if that matters that much or not in this case)
I am doing this by reading the file data (bytes), wrapping it in an object that contains some other details (user id, etc..) and sending this object over ObjectOutputStream
to the server.
It seems everything works fine until the byte array of the file is over a certain size (not sure what this strange threshold is yet but it seems 645KB is already too much). Then, the server throws a StreamCorruptedException
when trying to read the object from the ObjectInputStream
and closes the socket.
The code of the object message containing the file bytes:
public class MessageUploadFile extends MessageToServer {
private static final long serialVersionUID = 2356276507283427913L;
private String _destId;
private TransferDetails _td;
private byte[] _fileData;
public MessageUploadFile(String srcId, TransferDetails td, byte[] fileData){
super(srcId);
_destId = td.getDestinationId();
_td = td;
_fileData = fileData;
}
The client side socket and streams initialization:
clientSocket= new Socket(host, port);
output = new ObjectOutputStream(clientSocket.getOutputStream());
input = new ObjectInputStream(clientSocket.getInputStream());
Sending the message using:
output.writeObject(msg);
These are the streams initialization on the server side:
input = new ObjectInputStream(clientSocket.getInputStream());
output = new ObjectOutputStream(clientSocket.getOutputStream());
Reading the message using:
msg = input.readObject();
StreamCorruptedException
A Java StreamCorruptedException can be thrown while deserialising data. It essentially occurs in one of two main cases:
you try to open an ObjectInputStream around some data that wasn't actually written using an ObjectOutputStream
OR
During a readObject() operation, the stream gets in the "wrong place".
From java docs:
Thrown when control information that was read from an object stream violates internal consistency checks.
But I got this exception with large message and moved to byte array solution.
Have a look at this article:http://www.javamex.com/tutorials/io/StreamCorruptedException.shtml
In summary, convert Object to and from byte array and re-create it.
这篇关于Java的StreamCorruptedException超过一定尺寸发送包含字节数据对象时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!