gpt4 book ai didi

java - Smack FileTransferManager.addFileTransferListener 从未被调用

转载 作者:行者123 更新时间:2023-12-01 16:57:31 25 4
gpt4 key购买 nike

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

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 字节。

关于java - Smack FileTransferManager.addFileTransferListener 从未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61566220/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com