inputStream读取方法不断读取0

时间:2016-05-08 16:32:55

标签: java tcp bytearray outputstream

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

//此方法将文件写入主文件

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

//此方法将文件作为字节数组读入主服务器,将字节数据从主服务器读入从服务器

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

1 个答案:

答案 0 :(得分:1)

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

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