Android使用smack进行文件传输

发布于 2024-12-05 02:41:46 字数 2487 浏览 1 评论 0原文

我正在开发聊天应用程序,我必须使用 smack api 实现文件传输。 我能够连接到开火服务器,也可以与另一个客户端聊天。但我不知道如何实现文件传输。我找到了一个代码片段,但我也无法使用它发送它。以下是代码我正在使用的片段:

public void SendFile(final String Receiver, final String Directory) {
        Thread thread = new Thread() {
            public void run() {
            ServiceDiscoveryManager sdm = ServiceDiscoveryManager
            .getInstanceFor(connection);
            if (sdm == null)
                sdm = new ServiceDiscoveryManager(connection);
            sdm.addFeature("http://jabber.org/protocol/disco#info");
            sdm.addFeature("jabber:iq:privacy");
            // Create the file transfer manager
            FileTransferManager manager = new FileTransferManager(
                    connection);
            FileTransferNegotiator
            .setServiceEnabled(connection, true);

            // Create the outgoing file transfer
            OutgoingFileTransfer transfer = manager
            .createOutgoingFileTransfer("alok@chd-akumar4" );
            Log.i("transfere file", "outgoingfiletransfere is created");
            try {
                OutgoingFileTransfer.setResponseTimeout(30000);
                transfer.sendFile(new File(Directory), "Description");
                Log.i("transfere file", "sending file");
                while (!transfer.isDone()) {
                    try {
                        Thread.sleep(1000);
                        Log.i("transfere file", "sending file status "
                                + transfer.getStatus() + "progress: "
                                + transfer.getProgress());
                        if (transfer.getStatus() == org.jivesoftware.smackx.filetransfer.FileTransfer.Status.error) {
                            transfer.cancel();
                            Log.e("","EEEEEERRRRRRRROOORRRRR");
                            break;
                        }
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        Log.e("aaaaaaaaaaaaaaa","aaaa"+e);
                        e.printStackTrace();
                    }
                }
            } 
            catch (XMPPException e) {
                // TODO Auto-generated catch block
                Log.e("aaaaaaaaaaaaaaa","aaaa"+e);
                e.printStackTrace();
            }
            Log.i("transfere file", "sending file done");
        }
        };
        thread.start();
    }

任何人都可以帮助我找到解决方案..

I am working on chatting application and i have to implement file transfer using smack api.
I am able to Connect to the open fire server and can also chat with another client.But i dont know how to implement file transfer..I have found a code snippet but i am not ableto send it using that also.Following is the code snippet i am using:

public void SendFile(final String Receiver, final String Directory) {
        Thread thread = new Thread() {
            public void run() {
            ServiceDiscoveryManager sdm = ServiceDiscoveryManager
            .getInstanceFor(connection);
            if (sdm == null)
                sdm = new ServiceDiscoveryManager(connection);
            sdm.addFeature("http://jabber.org/protocol/disco#info");
            sdm.addFeature("jabber:iq:privacy");
            // Create the file transfer manager
            FileTransferManager manager = new FileTransferManager(
                    connection);
            FileTransferNegotiator
            .setServiceEnabled(connection, true);

            // Create the outgoing file transfer
            OutgoingFileTransfer transfer = manager
            .createOutgoingFileTransfer("alok@chd-akumar4" );
            Log.i("transfere file", "outgoingfiletransfere is created");
            try {
                OutgoingFileTransfer.setResponseTimeout(30000);
                transfer.sendFile(new File(Directory), "Description");
                Log.i("transfere file", "sending file");
                while (!transfer.isDone()) {
                    try {
                        Thread.sleep(1000);
                        Log.i("transfere file", "sending file status "
                                + transfer.getStatus() + "progress: "
                                + transfer.getProgress());
                        if (transfer.getStatus() == org.jivesoftware.smackx.filetransfer.FileTransfer.Status.error) {
                            transfer.cancel();
                            Log.e("","EEEEEERRRRRRRROOORRRRR");
                            break;
                        }
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        Log.e("aaaaaaaaaaaaaaa","aaaa"+e);
                        e.printStackTrace();
                    }
                }
            } 
            catch (XMPPException e) {
                // TODO Auto-generated catch block
                Log.e("aaaaaaaaaaaaaaa","aaaa"+e);
                e.printStackTrace();
            }
            Log.i("transfere file", "sending file done");
        }
        };
        thread.start();
    }

can any one help me finding the solution..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女净妖师 2024-12-12 02:41:46

如果您使用的是 3.2.x 版本,则存在文件传输的已知问题。尝试使用 3.1 看看是否可以解决您的问题。

If you are using version 3.2.x, there is a known problem with file transfer. Try using 3.1 to see if it fixes your problem.

通知家属抬走 2024-12-12 02:41:46

现在我们有 4.1.0 和更新的 API。

FileTransferRequest 和 StreamInitiation 现在使其变得更加容易。

请检查最新的 Smack 版本。

https://www.igniterealtime.org/builds /smack/docs/4.1.0/documentation/extensions/filetransfer.html

Now we have 4.1.0 with updated API's.

FileTransferRequest and StreamInitiation makes it more easy now.

Please check with latest Smack version.

https://www.igniterealtime.org/builds/smack/docs/4.1.0/documentation/extensions/filetransfer.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文