【问题标题】:something wrong when server receive a file from client?服务器从客户端接收文件时出现问题?
【发布时间】:2016-01-19 12:39:06
【问题描述】:

我正在尝试将文件从客户端发送到服务器,然后服务器响应客户端。服务器已收到文件,但客户端没有收到响应。

这是我的主要和客户端代码:

public static void main(String[] args) throws IOException {
    startServer();
    startSender();
}

public static void startSender() {
    (new Thread() {
        @Override
        public void run() {
            try {
                Socket client = new Socket("localhost", 10000);
                ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
                ObjectInputStream ois = new ObjectInputStream(client.getInputStream());

                //sent a file
                FileInputStream fis = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\constructor.jpg"));
                byte[] buffer = new byte[1024];
                int length = 0;
                while((length = fis.read(buffer, 0, buffer.length)) > 0){
                    oos.write(buffer, 0, length);
                    oos.flush();
                }
                //response
                String response = (String) ois.readObject();


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

        }
}).start();

}

服务器:

public static void startServer() {
    (new Thread() {
        @Override
        public void run() {
            ServerSocket server = null;
            Socket client = null;
            ObjectInputStream ois = null;
            ObjectOutputStream oos = null;

                try {
                    server = new ServerSocket(10000);
                    client = server.accept();
                    System.out.println("Connected!");
                    ois = new ObjectInputStream(client.getInputStream());
                    oos = new ObjectOutputStream(client.getOutputStream());

                    //receive the file
                    FileOutputStream fos = new FileOutputStream(new File("E:\\Java\\server\\constructor1.jpg"));
                    byte[] sendBytes = new byte[1024];
                    while(true) {
                        int read =0;
                        read = ois.read(sendBytes);
                        if(read == -1)
                            break;
                        fos.write(sendBytes, 0, read);
                        fos.flush();
                    }
                    //response
                    oos.writeObject("true");
                    oos.flush();

                } catch (IOException e1) {
                    e1.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
        }
    }).start();
}

怎么了?

【问题讨论】:

    标签: java sockets


    【解决方案1】:

    您必须在 ObjectInputStream 之前创建 ObjectOutputStream。见下文:

    ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
    ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
    

    这应该可以解决您输出 TEST 的问题。

    【讨论】:

    • 不输出TEST,他在sender中按顺序创建流,足以克服链接问题中提到的问题,不是这个问题,否则他不会'没有说服务器收到了文件。请阅读他的问题。
    • @EJP 在您不喜欢我的回答之前,请注意我的回答是在 OP 修改他的问题之前发布的。 stackoverflow.com/posts/34867416/revisions
    • @EJP 我的答案不仅正确,而且我什至测试了代码。完全有效。在我阅读的上下文中再次阅读问题,并考虑消除您的不喜欢。谢谢。
    • @EJP 对不起!!!我之前确实对我的问题进行了修改。这位帖子所有者确实解决了我的部分问题。所以请删除不喜欢的。谢谢你的回答,现在我的问题已经解决了。
    【解决方案2】:

    您的接收者正在读取流直到流结束,但发送者没有关闭套接字,因此流的结束永远不会发生。您将需要以某种方式更改您的发送协议:

    • 发送文件后关闭socket输出,但只能发送一个文件
    • 在文件之前发送文件长度,在接收器处读取它,然后在接收器处只读取恰好那么多字节。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-12
      • 2012-08-11
      • 1970-01-01
      • 2012-06-14
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多