gpt4 book ai didi

java.io.StreamCorruptedException : invalid stream header: 75720002

转载 作者:搜寻专家 更新时间:2023-10-31 20:15:35 24 4
gpt4 key购买 nike

我正在创建一个服务器-客户端应用程序,其中服务器向所有连接的客户端发送一个 pdf 文件。问题是我得到了这个错误,我搜索了一个解决方案但没有得到任何解决方案。这是错误

 java.io.StreamCorruptedException: invalid stream header: 75720002
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:782)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:279)
at StudentThread.run(StudentThread.java:102)

服务器发送代码如下:

public void run()
{
try
{
String modifiedSentence;
in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
inFromServer = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream())), true);
//String sentence;
System.out.println("TeachID. "+id);
modifiedSentence = in.readLine();
System.out.println("Student "+id+" says:"+modifiedSentence);
arrS=modifiedSentence.split(" ");
out.println("Hello "+arrS[2]+","+id);
studName=arrS[2];
((DefaultListModel) Teacher.made_list.getModel()).addElement(studName);

while( true )
{
modifiedSentence = in.readLine();
arrS=modifiedSentence.split(" ");
if(arrS[0].equals("AcceptFile"))
{
try
{
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
byte[] buffer = (byte[])ois.readObject();
String pic="copyServer"+id+".pdf";
System.out.println(pic);
FileOutputStream fos = new FileOutputStream(pic);
fos.write(buffer);
fos.flush();
fos.close();
}
catch(Exception e)
{
System.out.println("Exception writing");
}

}

}
catch (IOException e)
{
}
finally
{
try
{
socket.close();
}
catch(IOException e)
{
}
}
}
public void sendF(String fn,Teacher teach)
{
try{
out.println("AcceptFile,");
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()) ;
FileInputStream fis = new FileInputStream(fn);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
//ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream()) ;
oos.writeObject(buffer);
oos.flush();
fis.close();
}
catch(Exception e){
e.printStackTrace();
}
}
public void sendThread(String elem, Teacher teach)
{

out.println(elem);
//System.out.println ("Thread id is " + this.id);
System.out.println(this.socket.getInetAddress());
}

客户端接收代码如下:

public void run() 
{
try
{

out=new PrintWriter(socket.getOutputStream(), true);
out.println("Hello Server "+name+",");

String modifiedSentence;
BufferedReader inFromServer = new BufferedReader( new InputStreamReader( socket.getInputStream() ) );
modifiedSentence = inFromServer.readLine();
System.out.println( modifiedSentence );

arrT=modifiedSentence.split(",");
if(arrT[0].equals("Hello "+name))
{
studId=Integer.parseInt(arrT[2]);
System.out.println("My Id="+studId);
}

while( true )
{
modifiedSentence = inFromServer.readLine();
System.out.println( modifiedSentence );

arrT=modifiedSentence.split(",");
if(arrT[0].equals("AcceptFile"))
{
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
byte[] buffer = (byte[])ois.readObject();
String pic="copyServer"+studId+".gif";
System.out.println(pic);
FileOutputStream fos = new FileOutputStream(pic);
fos.write(buffer);
fos.flush();
fos.close();
}
}
}
catch( Exception e )
{
e.printStackTrace();
}
}

最佳答案

BufferedReader 可以从套接字中缓冲比您已经读取的更多的数据。所以在客户端,您的 byte[] 的 header 可能已经被您的 inFromServer 读取器读取并缓冲,并且您的 ObjectInputStream 将无法使用

不要做那种事。要么“手动”执行所有编码(marshal)处理(使用 PrintWriter/BufferedReader 对),要么使用 ObjectOutputStream/ 通过对象序列化完成所有操作>对象输入流

关于java.io.StreamCorruptedException : invalid stream header: 75720002,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6315247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com