Java 套接字在写入字节数组后只得到 NUL 字符

Java socket getting only NUL characters after writing byte array

我正在尝试通过 byte 数组将文件从 客户端 分块发送到 服务器 。我正在使用 ObjectInputStream。写入有效且文件大小匹配但是当我打开文件时,我只得到一个空白文本文件(在 IDE 中打开时显示 NUL,NUL,NUL...)。

服务器代码:

try(
    FileOutputStream fileOut = new FileOutputStream(file);
    ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
    ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
){
byte[] arr = new byte[chunkSize];
try {
    int len = 0;
    long bytesRead = 0;

    byte[] bytes = new byte[chunkSize];
    int chunkNo = 1;


    while(true)
    {
        len = in.read(bytes,0, chunkSize);
        System.out.println();
        if(len < 0)
            break;

        fileOut.write(arr, 0, len);

        bytesRead += len;

        out.writeObject(Server.CHUNK_ACKNOWLEDGE_MSG);
        String ackReply = (String) in.readObject();

        if(ackReply.equalsIgnoreCase((Server.UPLOAD_ACKNOWLEDGE_RECEIVE_TIMEOUT_MSG))){
            if(Server.DEBUG)
                System.out.println(fileName + " send timeout.");

            deleteFile();
            break;
        }else if (ackReply.equalsIgnoreCase(Server.UPLOAD_COMPLETE_MSG)){
            if(bytesRead != fileSize){

                System.out.println(fileName + " File size mismatch");
                deleteFile();
                break;
            }else{
                System.out.println( fileName + " File written");
                break;
            }
        }
    
    }
}catch (IOException ioe){
    if(Server.DEBUG)
        ioe.printStackTrace();
}

客户代码:

 try(
        FileInputStream fileInput = new FileInputStream(file);
        ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
        ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
    ){
        byte[] arr = new byte[chunkSize];
        try {
            int len = 0;
            int bytesRead = 0;
            int chunkCount = 1;
            while((len = fileInput.read(arr, 0, chunkSize)) != -1)
            {
                out.write(arr, 0, len);
                out.flush();

                bytesRead += len;

                }
                    try {
                        System.out.println("wait ack");

                        socket.setSoTimeout(timeout);
                        String ack = (String) in.readObject();
                        System.out.println(ack);

                        if(bytesRead >= fileSize){
                            out.writeObject(Server.UPLOAD_COMPLETE_MSG);
                            System.out.println(Server.UPLOAD_COMPLETE_MSG);
                            break;
                        }else{
                            out.writeObject(Server.CHUNK_ACKNOWLEDGE_MSG);
                        }
                    }catch (SocketTimeoutException e){
                        out.writeObject(Server.UPLOAD_ACKNOWLEDGE_RECEIVE_TIMEOUT_MSG);
                        System.out.println(Server.UPLOAD_ACKNOWLEDGE_RECEIVE_TIMEOUT_MSG);

                        break;
                    }finally {
                        socket.setSoTimeout(0);
                    }
                }
            }

        }catch (IOException ioe){
            ioe.printStackTrace();
        }
    }catch (FileNotFoundException fnfe){
        System.out.println("No such file: " + fileName);
    }catch (Exception e){
        e.printStackTrace();
    }
    finally {
        try {
            socket.close();
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

我尝试将 byte 数组写入客户端的另一个文件,副本是相同的。所以问题一定是在通过套接字发送数据的过程中。

Server.X_MSG只是一个常量字符串。我不知道在同一个 ObjectInputStream 上混合 readobject()read(bytearray) 是否会导致任何问题。

可能是因为 fileOut.write(arr, 0, len); 使用 arrlen = in.read(bytes,0, chunkSize); 使用 bytes?它们不是同一个数组。