【问题标题】:Java File Transfer getting stuck halfwayJava 文件传输中途卡住
【发布时间】:2009-12-05 20:31:53
【问题描述】:

我正在尝试向客户端发送一个文件(作为字节数组发送的图像),然后服务器应该接收到所述字节数组以进一步使用它。但是,当我单击“发送”以发送图像时,文件传输开始(因为我在桌面上获得了一个 sentImage.jpg),但由于某种原因我无法弄清楚并且图像永远不会正确发送。

这是从服务器接收的部分(它已经接受了连接):

 public void run(){
   try {
          byte[] receivedData = new byte[1024];
          BufferedInputStream bis = new BufferedInputStream(client.getInputStream());
         // while(bis.read() != -1){
          s.acquireUninterruptibly();
          BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\Admin\\Desktop\\sentImage.jpg"));
          while ((incoming = bis.read(receivedData)) != -1) {
              bos.write(receivedData, 0, incoming);
          }
          s.release();
          n.release();
          bis.close();
          bos.flush();
        // }
      } catch (IOException e) {
          e.printStackTrace();
      }
}

而客户端正在这里发送:

public void sendImageResult() {
            new Thread(new Runnable() {
                public void run() {
                    try {
                        int inside = 0;

                        Socket socket = new Socket("localhost", 4444);

                        File myImageFile = new File("C:\\Users\\Admin\\Desktop\\test.jpg");
                        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myImageFile));
                        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream( ));
                        byte[] byteArray = new byte[1024];
                        while ((inside = bis.read(byteArray)) != -1){
                            bos.write(byteArray,0,inside);
                        }
                        bis.close();
                        bos.flush();
                    } catch (UnknownHostException ex) {
                        System.out.println("No se pudo establecer la conexión.");
                        ex.printStackTrace();
                    } catch (FileNotFoundException fnf){
                        fnf.printStackTrace();
                    } catch(IOException ioe){
                        ioe.printStackTrace();
                    }

                }
            }).start();
        }

【问题讨论】:

  • 最好将 close 和 flush 放在 finally 块中。否则,如果发生错误,可能会发生不好的事情。
  • 我应该这样做吗,bos.flush();然后 bos.close(); ??
  • 挑起事态,并为挂起的线程生成堆栈跟踪。将它们附加到您的问题中(在来源上带有行号)。
  • 我没有收到任何错误,发送文件时程序只是“挂起”
  • 尝试为 Exception 添加另一个 catch 块。目前,如果抛出的异常不是指定类型之一,您可能不会看到错误,因为它作为单独的线程运行。

标签: java sockets file-transfer


【解决方案1】:

用于写入磁盘的 OutputStream (bos) 似乎没有被关闭。这可能会导致意想不到的结果。

【讨论】:

【解决方案2】:

正如jt所说,写入磁盘的OutputStream没有被关闭,但OutputStream也没有被用来发送数据,Socket也没有从发送端关闭。发送端可能正在 tcp 级别缓冲数据,在发送最后一个数据包之前等待更多字节。您正在调用flush,但这可以忽略不计,它不能保证像您期望的那样工作。要尝试的另一件事是在 Socket 上调用 shutdownOutput 并查看是否会强制它刷新。您也可以在打开 Socket 时尝试 setTcpNoDelay(true)。如果这些都不起作用,请获取一个 tcp 跟踪程序(我喜欢 tcpdump)并使用它来查看数据包是否真正被发送,它至少会将其缩小到发送或接收端。

【讨论】:

    猜你喜欢
    • 2020-10-21
    • 2020-12-08
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    相关资源
    最近更新 更多