java.io.StreamCorruptedException:无效的流标头:75720002

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6315247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:20:41  来源:igfitidea点击:

java.io.StreamCorruptedException: invalid stream header: 75720002

java

提问by Fatema Mohsen

i am creating a server-client application where the server sends a pdf file to the all connected clients. The problem is i get this error and i searched for a solution but couldn`t get any. This is the error

我正在创建一个服务器-客户端应用程序,其中服务器向所有连接的客户端发送一个 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)

Here is the server sending code:

这是服务器发送代码:

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());
}

Here is the client receiving code:

这是客户端接收代码:

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();
    }
}

回答by Mat

BufferedReadercan buffer more data from the socket than you've read yet. So on the client side, the header of your byte[]has probably already been read and buffered by your inFromServerreader, and will not be available to your ObjectInputStream.

BufferedReader可以从套接字缓冲比您读过的更多的数据。因此,在客户端,byte[]您的inFromServer阅读器可能已经读取并缓冲了您的标题,并且您的ObjectInputStream.

Don't do that kind of thing. Either do all your marshaling "manually" (using the PrintWriter/BufferedReaderpair), or do it all with object serialization with ObjectOutputStream/ObjectInputStream.

不要做那种事。要么做你编组“手动”(使用PrintWriter/BufferedReader对),或做这一切与对象序列化ObjectOutputStream/ ObjectInputStream