Smack FileTransferManager.addFileTransferListener 从未被调用

Smack FileTransferManager.addFileTransferListener is never called

我正在使用 smack 4.3.4 开发一个独立的 Java 测试应用程序。我使用 In-Band 进行文件传输:

FileTransferManager fileTransferManager = FileTransferManager.getInstanceFor(connection);
        FileTransferNegotiator.IBB_ONLY = true;
        OutgoingFileTransfer fileTransfer = null;
        try {
            fileTransfer = fileTransferManager.createOutgoingFileTransfer(JidCreate.entityFullFrom(buddyJID + "/Spark"));
        } catch (XmppStringprepException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }
       if (fileTransfer != null) {
            OutgoingFileTransfer.setResponseTimeout(500);
            ...
           try {
                    fileTransfer.sendFile(file, "sending attachment...");
                } catch (SmackException ex) {
                    LOG.log(Level.SEVERE, null, ex);
                }
           ...

我监控文件传输并正确发送。

INFO: Initializing connection to server localhost port 5222 [Wed Apr 29 15:36:25 CEST 2020]
INFO: Connected: true [Wed Apr 29 15:36:45 CEST 2020]
INFO: user001 authenticated? true [Wed Apr 29 15:36:46 CEST 2020]
...
INFO: Sending attachment 'test.txt' to user user002@localhost [Wed Apr 29 15:36:55 CEST 2020]
INFO: status is:Initial [Wed Apr 29 15:36:55 CEST 2020]
INFO: status is:Initial [Wed Apr 29 15:36:55 CEST 2020]
INFO: File transfer status: Negotiating Transfer, progress: 0.0 [Wed Apr 29 15:36:55 CEST 2020]
INFO: test.txt has been successfully transferred. [Wed Apr 29 15:36:56 CEST 2020]
INFO: The file transfer is done. [Wed Apr 29 15:36:56 CEST 2020]
INFO: Attachment test.txt sent from user001@localhost to user002@localhost [Wed Apr 29 15:36:56 CEST 2020]

当我的用户连接并登录到 Openfire 时,甚至在开始发送附件之前,我都会添加一个侦听器来侦听文件传输:

final FileTransferManager manager = FileTransferManager.getInstanceFor(connection);
//        FileTransferNegotiator.setServiceEnabled(connection, true);   
        manager.addFileTransferListener((FileTransferRequest request) -> {
            // Check to see if the request should be accepted
            if (request.getFileName() != null) {
                try {
                    // Accept it
                    IncomingFileTransfer transfer = request.accept();
//                    monitorFileTransfer(transfer, "");
                    try (InputStream fileReceived = transfer.receiveFile();
                            BufferedInputStream bis = new BufferedInputStream(fileReceived)) {
                    ...
              } else {
                try {
                    // Reject it
                    request.reject();
                    LOG.warning("File rejected " + request.getFileName());
                } catch (SmackException.NotConnectedException | InterruptedException ex) {
                    LOG.log(Level.SEVERE, null, ex);
                }
            }

但是,永远不会调用侦听器。我需要在特定时刻添加监听器吗?还有什么我一直想念的吗?结果是文件传输被发送到 Openfire,并且永远不会被消耗。

最后一节:

<iq xmlns="jabber:client" to="user001@localhost/aktuu2n806" from="localhost" id="679-1085" type="get">
  <query xmlns="jabber:iq:version"/>
</iq>
<iq xmlns="jabber:client" to="localhost" id="679-1085" type="error">
  <error xmlns="jabber:client" type="modify">
    <not-acceptable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
  </error>
</iq>

我的代码基于documentation example。我需要配置其他东西吗?由于它们是作为带内传输的,所以我不应该这样做。

提前感谢您的及时回复。

文件传输实际上并没有发生,这就是为什么监听器在监听但什么也没收到。

要发送文件,您需要收件人的完整 JID。如果您使用 smack,则没有物理 XMPP 客户端(如 spark)。在这种情况下,您可以使用“Smack”或“Resource”(参见 here)。

fileTransfer = fileTransferManager.createOutgoingFileTransfer(JidCreate.entityFullFrom(buddyJID + "/Smack")));

但是,我仍然得到一个错误:

XMPP error reply received from user002@localhost/Smack: XMPPError: not-allowed - cancel

文件没有发送。

更新:当然,问题与 smack 无关。问题是我创建的工具正在读取和写入同一个文件,结果文件被删除,错误是因为请求 smack 传输一个空文件。因此,如果您看到此错误,请先检查您尝试传输的文件;它可能有 0 个字节。