invalid stream header: 00000000

Asked

Viewed 468 times

0

Good evening. I am trying to read all the files from a directory on one machine (client) and send to another machine (server) using Java Socket. However, at the end of the transfer I get the following error in the "server" application: java.io.Streamcorruptedexception: invalid stream header: 00000000.

Java client (Sends a file (serialized) at a time through).

public boolean listar (File file) throws FileNotFoundException, IOException {

        FileInputStream fis;

        if(file.isDirectory()) {

            File info = new File(file.getPath());
            File afile[] = info.listFiles();

            if (afile.length != 0) {
                int i = 0;

                for (int j = afile.length; i < j; i++) {
                    File arquivos = afile[i];
                    if(arquivos.getName() != "") {

                        //System.out.println(file.getPath() + "\\" + arquivos.getName());
                        long kbSize = arquivos.length() / 1024;

                        byte[] bFile = new byte[(int) arquivos.length()];
                        fis = new FileInputStream(arquivos);
                        fis.read(bFile);
                        fis.close();

                        arquivo = new Diretorio();
                        arquivo.setConteudo(bFile);
                        arquivo.setDataHoraUpload(new Date());
                        arquivo.setNome(arquivos.getName());
                        arquivo.setTamanhoKb(kbSize);
                        arquivo.setDiretorioDestino(diretorioServidor);

                        BufferedOutputStream bf = new BufferedOutputStream(conn.getOutputStream());

                        byte[] bytea = serializarArquivo();
                        bf.write(bytea);
                        bf.flush();
                        bf.close();
                        conn.close();

                    }

                }
            } 

            String[] subDiretorio = file.list();

            if(subDiretorio != null) {
                for (String dir : subDiretorio) {
                    listar(new File(file + File.separator + dir));
                }
            }
        }
        return true;
    }

Server.java (Takes one file at a time and writes the file to the destination directory)

public class ServerStart implements Runnable 
    {
        @Override
        public void run() 
        {
            try 
            {
                //server = new ServerSocket(3587, 1, InetAddress.getLocalHost());
                //conn = server.accept();
                ServerSocket srvSocket = new ServerSocket(3000);
                System.out.println("Aguardando envio de arquivo ...");
                Socket socket = srvSocket.accept();

                while (true) 
                {
                    try
                    {                      

                        byte[] objectAsByte = new byte[socket.getReceiveBufferSize()];
                        BufferedInputStream bf = new BufferedInputStream(socket.getInputStream());
                        bf.read(objectAsByte);

                        /*Diretorio diretorio = (Diretorio) getObjectFromByte(objectAsByte);

                        String dir = diretorio.getNome();

                        System.out.println(dir);*/

                        Diretorio diretorio = (Diretorio) getObjectFromByte(objectAsByte);

                        String dir = diretorio.getDiretorioDestino().endsWith("/") ? diretorio.getDiretorioDestino() + diretorio.getNome() : diretorio.getDiretorioDestino() + "\\" + diretorio.getNome();
                        System.out.println("Escrevendo arquivo " + dir);

                        FileOutputStream fos = new FileOutputStream(dir);
                        fos.write(diretorio.getConteudo());
                        //fos.close();

                        Thread.sleep(200);

                    } catch (Exception ex) {

                        try {
                            Thread.sleep(3000);
                            System.exit(0);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }       
                }
            }
            catch (Exception ex)
            {
              try
                {
                    Thread.sleep(3000);
                    System.exit(0);
                }catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static Object getObjectFromByte(byte[] objectAsByte) {
        Object obj = null;
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;

        try {
                 bis = new ByteArrayInputStream(objectAsByte);
                 ois = new ObjectInputStream(bis);
                 obj = ois.readObject();

                 bis.close();
                 ois.close();

        } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
        } catch (ClassNotFoundException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();           
        }                 

        return obj;

    }

The records contained in the client folder are sent to the server folder, however the cited error is triggered and the applications are closed.

index class:

public class Diretorio implements Serializable {

    private static final long seralVersionUID = 2L;

    private String nome;
    private byte[] conteudo;
    private transient long tamanhoKb;
    private transient Date dataHoraUpload;
    private transient long data;
    private transient String ipDestino;
    private transient String ipLocal;
    private transient String portaDestino;
    private String diretorioDestino;
    private transient String status;
    private boolean pronto;

    public synchronized String getNome() throws InterruptedException {

        while(pronto) {
            wait();
        }

        notify();

        return nome;
    }

    public synchronized void setNome(String nome) throws InterruptedException {
        while(pronto) {
            wait();
        }

        this.nome = nome;

        notify();
    }

    public synchronized byte[] getConteudo() throws InterruptedException {
        while(pronto) {
            wait();
        }

        notify();

        return conteudo;

    }

    public synchronized String getStatus() throws InterruptedException {

        while(pronto) {
            wait();
        }

        notify();

        return status;
    }

    public synchronized void setStatus(String status) throws InterruptedException {
        while(pronto) {
            wait();
        }

        this.status = status;

        notify();
    }

    public synchronized void setConteudo(byte[] conteudo) throws InterruptedException {
        while(pronto) {
            wait();
        }

        this.conteudo = conteudo;

        notify();
    }

    public synchronized long getTamanhoKb() throws InterruptedException {
        while(pronto) {
            wait();
        }

        notify();

        return tamanhoKb;
    }

    public synchronized void setTamanhoKb(long tamanhoKb) throws InterruptedException {
        while(pronto) {
            wait();
        }

        this.tamanhoKb = tamanhoKb;

        notify();
    }

    public synchronized long getData() throws InterruptedException {
        while(pronto) {
            wait();
        }

        notify();

        return data;
    }

    public synchronized void setData(long data) throws InterruptedException {
        while(pronto) {
            wait();
        }

        this.data = data;

        notify();
    }

    public synchronized Date getDataHoraUpload() throws InterruptedException {
        while(pronto) {
            wait();
        }

        notify();

        return dataHoraUpload;
    }

    public synchronized void setDataHoraUpload(Date dataHoraUpload) throws InterruptedException {
        while(pronto) {
            wait();
        }

        this.dataHoraUpload = dataHoraUpload;

        notify();
    }

    public synchronized String getIpDestino() throws InterruptedException {
        while(pronto) {
            wait();
        }

        notify();

        return ipDestino;
    }

    public synchronized void setIpDestino(String ipDestino) throws InterruptedException {
        while(pronto) {
            wait();
        }

        this.ipDestino = ipDestino;

        notify();
    }

    public synchronized String getIpLocal() throws InterruptedException {
        while(pronto) {
            wait();
        }

        notify();

        return ipLocal;
    }

    public synchronized void setIpLocal(String ipDestino) throws InterruptedException {
        while(pronto) {
            wait();
        }

        this.ipLocal = ipLocal;

        notify();
    }

    public synchronized String getPortaDestino() {
        while(pronto) {
            try {
                wait();
            } catch (InterruptedException ex) {
                Logger.getLogger(Diretorio.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        notify();

        return portaDestino;
    }

    public synchronized void setPortaDestino(String portaDestino) throws InterruptedException {
        while(pronto) {
            wait();
        }

        this.portaDestino = portaDestino;

        notify();
    }

    public synchronized String getDiretorioDestino() throws InterruptedException {
        while(pronto) {
            wait();
        }

        notify();

        return diretorioDestino;
    }

    public synchronized void setDiretorioDestino(String diretorioDestino) throws InterruptedException {
        while(pronto) {
            wait();
        }

        this.diretorioDestino = diretorioDestino;

        notify();
    }

}
  • Put in question your directory class too, please

  • @Sorack good afternoon, sir. Yesterday I was able to solve this problem by removing the snippet: bis.close(); ois.close(); From the "getObjectFromByte" function. However now he does not write all the files coming. He receives but does not write in the served directory and does not report errors. I believe it’s something to do with synchronization.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.