Java DataInputStream read()始终返回-1

Java DataInputStream read()始终返回-1,java,file,server,client,datainputstream,Java,File,Server,Client,Datainputstream,我有一个java的客户机-服务器程序,客户机向服务器发送请求,服务器通过发送文件进行响应。 与服务器的连接正确,客户端发送的第一个请求由服务器正确解释 此时,服务器读取一个特定的文件,该文件似乎读得很好,因为写操作发送了正确的字节数和字符串(通过打印字节数和字符串进行测试) 发送第一条消息后,客户机等待接收文件,但一旦收到消息,读取返回-1,就好像它已读取EOF,我不明白原因,如何解决它 方法在服务器上发送文件的时间 private void sendFile(Socket client, St

我有一个java的客户机-服务器程序,客户机向服务器发送请求,服务器通过发送文件进行响应。 与服务器的连接正确,客户端发送的第一个请求由服务器正确解释

此时,服务器读取一个特定的文件,该文件似乎读得很好,因为写操作发送了正确的字节数和字符串(通过打印字节数和字符串进行测试)

发送第一条消息后,客户机等待接收文件,但一旦收到消息,读取返回-1,就好像它已读取EOF,我不明白原因,如何解决它

方法在服务器上发送文件的时间

private void sendFile(Socket client, String path, DataOutputStream writer) {
        File file = new File(path);
        byte[] bytes = new byte[1024];
        InputStream in = null;
        try {
            in = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        int count;
        try {
            while ((count = in.read(bytes)) > 0) {
                System.out.println("nBytes " + count);
                String tmp = new String(bytes);
                System.out.println(tmp);

                //i'm reading from a json file this string
                //[{"username":"user"},{"score":0},{"friendList":["us1","us2"]},{"password":"password"}]
                //and it is printed correctly

                writer.write(bytes, 0, count);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
客户端方法

public void getFile (String username) {
        // first of all connects correctly with the server and sends a message, the message is received correctly by the server, after it waits for a reply message

        Socket socket = connect2server();
        DataInputStream reader = null;
        BufferedWriter writer = null;


        try {
            reader = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
            writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //send message to the server using writer 

        OutputStream out = null;
        String file = username+".json";
        try {
            out = new FileOutputStream(file);
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        byte[] bytes = new byte[1024];

        int count;
        try {
            while ((count = reader.read(bytes)) >= 0) {
                System.out.println("nByte " + count);
                out.write(bytes, 0, count);
            }

            // the client is waiting correctly on the read but as soon as it receives a message, the read returns -1 as if it had only read an EOF and not the whole message

            System.out.println(count);
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        try {
            out.close();
            reader.close();
            writer.close();
            socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }

在您的
sendFile()
writer中,未关闭。您需要
flush()
然后
close()
写入程序,以确保创建输出。这可能会解决您的问题

关于
写入
,您没有关闭/刷新输出流,这反过来也不会释放与该流相关的任何资源。您有两个选择:

  • 关闭调用其flush方法的输出流,然后 然后调用其底层输出流的close方法
  • 刷新输出流缓冲区。缓冲主要是为了提高
    I/O
    性能
  • 从中可以看出,
    read()

    从输入流读取数据的下一个字节。值字节作为0到255范围内的整数返回。如果由于到达流的结尾而没有字节可用,则返回值-1。此方法会一直阻塞,直到输入数据可用、检测到流结束或引发异常为止。 子类必须提供此方法的实现

    返回: 数据的下一个字节,如果到达流的末尾,则为-1