如何使用XMPP聊天时设备之间发送文件(How to send file between devic

2019-10-22 20:08发布

感谢您关注过的问题...

我正在与ejabbered和asmack_4.1库的帮助下XMPP聊天应用。

我已经安装了PSI的Windows客户端,以与Android设备进行聊天。 我能够在设备之间进行文本聊天。 我也能够从设备发送文件,PSI客户端和PSI客户端Android设备。 但问题是将文件发送设备到设备。

我使用下面的代码来发送和接收文件:

发送文件:

public void fileTransfer(String filenameWithPath, Bitmap thumbnail, String userId) {
        // FileTransferManager manager = new FileTransferManager(connection);
        FileTransferManager manager = FileTransferManager.getInstanceFor(connection);
        //    OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer("usre2@myHost/Smack");
        OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(userId + "/Admin"); // "/Good" depends on server
//        OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(userId + "/Directors-iMac");

        File file = new File(filenameWithPath);
        try {
            transfer.sendFile(file, "test_file");
        } catch (SmackException e) {
            e.printStackTrace();
        }

        while (!transfer.isDone()) {
            if (transfer.getStatus().equals(FileTransfer.Status.error)) {
                System.out.println("ERROR!!! " + transfer.getError());
            } else if (transfer.getStatus().equals(FileTransfer.Status.cancelled)
                    || transfer.getStatus().equals(FileTransfer.Status.refused)) {
                System.out.println("Cancelled!!! " + transfer.getError());
            }
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        if (transfer.getStatus().equals(FileTransfer.Status.refused) || transfer.getStatus().equals(FileTransfer.Status.error)
                || transfer.getStatus().equals(FileTransfer.Status.cancelled)) {
            System.out.println("refused cancelled error " + transfer.getError());
        } else {
            System.out.println("Success");
        }
    }

文件接收:

public void fileReciever(AbstractXMPPConnection connection) {
        //**************************************

        // Create the file transfer manager
        final FileTransferManager managerListner = FileTransferManager.getInstanceFor(connection);

        //    FileTransferNegotiator.setServiceEnabled(connection, true);
        FileTransferNegotiator negotiator = FileTransferNegotiator.getInstanceFor(connection);

        Log.i("File transfere manager", "created");

        // Create the listener
        managerListner
                .addFileTransferListener(new FileTransferListener() {
                    @Override
                    public void fileTransferRequest(FileTransferRequest request) {

                        Log.i("Recieve File", "new file transfere request  new file transfere request   new file transfere request");

                        Log.i("file request", "from" + request.getRequestor());

                        IncomingFileTransfer transfer = request.accept();

                        Log.i(RECIEVE_FILE_ALERT_DIALOG, "accepted");

                        try {

                            transfer.recieveFile(new File("/sdcard/" + request.getFileName()));

                            while (!transfer.isDone() || (transfer.getProgress() < 1)) {

                                Log.i(RECIEVE_FILE_ALERT_DIALOG, "still receiving : " + (transfer.getProgress()) + " status " + transfer.getStatus());

                                if (transfer.getStatus().equals(FileTransfer.Status.error)) {
                                    // Log.i("Error file",
                                    // transfer.getError().getMessage());
                                    Log.i("Recieve File",
                                            "cancelling still receiving : "
                                                    + (transfer.getProgress())
                                                    + " status "
                                                    + transfer.getStatus());
                                    transfer.cancel();

                                    break;
                                }
                            }

                        } /*catch (XMPPException e) {

                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }*/ catch (SmackException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                });

        //**************************************
    }

注意:在文件传输()方法中,我使用下面一行:

OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(userId + "/Admin"); // "/Good" depends on server

我能够从Android设备将文件发送到PSI的桌面应用程序,反之亦然,但我不能Android设备之间传输文件。

文章来源: How to send file between device using xmpp while chat