inputStream read方法不断读取0

inputStream read method constantly reading 0

我首先将文件从客户端传输到我的主机,存储字节数组,然后发送到从机。从站存储字节数组的位置。但是当文件从客户端正确发送到主机但是当我将字节数组发送到从机它到从机时,输入流中的读取方法不断读取 0。

// 该方法将文件写入master

public void writeFile(File file) {
    try {
        this.write(String.valueOf(file.length()));

        byte[] bytearray = new byte[(int) file.length()];
        FileInputStream fin = new FileInputStream(file);
        BufferedInputStream bin = new BufferedInputStream(fin);
        bin.read(bytearray, 0, bytearray.length);
        BufferedOutputStream bos;
        OutputStream os = socket.getOutputStream();
        bos= new BufferedOutputStream(os);
        bos.write(bytearray, 0, bytearray.length);
        bos.flush();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

//该方法将文件以字节数组的形式读入master,将master的字节数组读入slave

public byte[] readFile() {

    byte[] bytearray = null;
    try {
        int currentTot = 0;
        int filesize = Integer.parseInt(this.read());
        System.out.println(filesize);
        bytearray = new byte[filesize];
        InputStream is = socket.getInputStream();
        int bytesRead;

        bytesRead = is.read(bytearray, 0, bytearray.length);
        currentTot = bytesRead;
        int count = 0;
        do {
            bytesRead = is.read(bytearray, currentTot, (bytearray.length - currentTot));
            if (bytesRead > 0) {
                currentTot += bytesRead;
                count = 0;
            } else {
                count++;
                System.out.println("count " + count);
            }
        } while (bytesRead > -1);
        System.out.println(currentTot);
        // bos.write(bytearray, 0, currentTot);
        // bos.flush();
        // bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bytearray;
}
//This method writes from the master to the slave 
public void writeByte(byte[] m) {
        this.write(String.valueOf(m.length));
        System.out.println("File side inside sender" + m.length);
        // byte[] bytearray = m;
        OutputStream os;
        try {
            os = socket.getOutputStream();
            os.write(m, 0, m.length);
            os.flush();
            //os.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

有趣的是,如果我在从我的主机发送我的字节数组后关闭我的输出流,它运行良好。但是我不能关闭流,因为奴隶需要进一步与主人沟通。提前致谢。

public void write(String output) {
    if (pw == null)
        this.openWriter();
    pw.println(output);
}
public String read() {
    try {
        if (br == null) {
            if (this.socket != null)
                br = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
        }
        return br.readLine();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

您误读了接收器中的文件长度。你得到零,所以你正在构造一个零长度字节数组,所以 read() returns 零。

您需要通过DataOutputStream.writeLong()发送长度并通过DataInputStream.readLong()读取。然后你的发送和接收代码也都是错误的。有关完整代码,请参阅 my answer here