Передача файлов XMPP с использованием smack api (получатель не получает запрос) - PullRequest
4 голосов
/ 28 марта 2012

Привет, я пытаюсь отправить файл.Но это не запускает передачу файла.при проверке я обнаружил, что слушатель получателя не получает запрос от отправителя.Вот код отправителя: (коды ruff gui .. только посмотрите на filetransfer thingy) import

java.awt.Color; 
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.tree.DefaultMutableTreeNode;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.ServiceDiscoveryManager;
import org.jivesoftware.smackx.filetransfer.FileTransfer;
import org.jivesoftware.smackx.filetransfer.FileTransferManager;
import org.jivesoftware.smackx.filetransfer.FileTransferNegotiator;
import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;
public class FileSelectionMainGUI {


    ConnectionConfiguration configGoogle = new ConnectionConfiguration("jabber.org", 5222);

    JFrame mainWindow;
    JLabel selectEmailLabel,selectFileLabel;
    JButton browseFilesButton,sendButton;
    JTextField browseField;
    JComboBox buddyListBox;
    JPanel contentPanel;
    Roster roster;
    FileTransferManager fileTransferManager;
    XMPPConnection connection;
    DecimalFormat transferringProgress = new DecimalFormat("#");
    XMPPFileManager receiveManager;

    public void createGUI()
    {
        mainWindow = new JFrame("Sender");
        mainWindow.setLayout(null);
        mainWindow.setSize(700, 600);
        mainWindow.setLocation(100, 50);

        contentPanel = new JPanel();
        contentPanel.setLayout(null);
        contentPanel.setBounds(0, 0, 700, 600);

        selectEmailLabel = new JLabel("Enter User Email ID");
        selectEmailLabel.setBounds(80, 325, 150, 30);
        contentPanel.add(selectEmailLabel);

        selectFileLabel = new JLabel("Select File");
        selectFileLabel.setBounds(96, 390, 150, 30);
        contentPanel.add(selectFileLabel);

        buddyListBox = new JComboBox();
        buddyListBox.addItem("receiver@jabber.org");
        buddyListBox.setBackground(Color.white);
        buddyListBox.setBounds(230, 325, 390, 30);
        contentPanel.add(buddyListBox);

        browseField = new JTextField();
        browseField.setText("testdoc.txt");
        browseField.setBounds(230, 390, 220, 30);
        contentPanel.add(browseField);

        browseFilesButton = new JButton("Browse File");
        browseFilesButton.setBounds(470, 390, 130, 30);
        contentPanel.add(browseFilesButton);

        sendButton = new JButton("Send File");
        sendButton.setBounds(470, 490, 130, 30);
        contentPanel.add(sendButton);
        sendButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                try {
                    sendFile(buddyListBox.getItemAt(0).toString(), browseField.getText());
                } catch (XMPPException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

        mainWindow.add(contentPanel);
        mainWindow.setVisible(true);

    }




    public void login(String userName,String password,String type_server)
        {


                    configGoogle.setSASLAuthenticationEnabled(true);
                    connection = new XMPPConnection(configGoogle);
                    XMPPConnection.DEBUG_ENABLED = true;

                    SASLAuthentication.supportSASLMechanism("PLAIN");
                    try
                    {

                        connection.connect();
                        connection.login(userName, password);
                        System.out.println("logged in...");
                    }
                    catch(Exception e)
                    {

                        e.printStackTrace();
                    }
                    new ServiceDiscoveryManager(connection);
                    fileTransferManager = new FileTransferManager(connection);
                    FileTransferNegotiator.setServiceEnabled(connection, true);

        }









    public void sendFile(String userID, String path) throws XMPPException {

        OutgoingFileTransfer fileTransfer = fileTransferManager.createOutgoingFileTransfer(userID);
        System.out.println("status is:"+fileTransfer.getStatus());
        try {
            fileTransfer.sendFile(new File(path), "this is the description");
            System.out.println("status is:"+fileTransfer.getStatus());
            System.out.println("sent .. just");
            while (!fileTransfer.isDone())
            {

                if (fileTransfer.getStatus() == FileTransfer.Status.refused)
                {
                    JOptionPane.showMessageDialog(null, "Could not send the file to " + userID + ".", "Failed", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                if (fileTransfer.getStatus() == FileTransfer.Status.error)
                {

                    JOptionPane.showMessageDialog(null, "Cannot send the file because an error occured during the process.", "Failed", JOptionPane.ERROR_MESSAGE);
                    return;
                }

            }

            System.out.println(fileTransfer.getFileName() + "has been successfully transferred.");

            System.out.println("The Transfer is " + fileTransfer.isDone());

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

    }


    public static void main(String args[])
    {
        FileSelectionMainGUI obj = new FileSelectionMainGUI();
        obj.createGUI();
        obj.login("senderjabberid", "password", "jabber");


    }

}

Теперь код для получателя: (ruff не подходит для gui и т.д.)

import java.awt.Color; 
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.tree.DefaultMutableTreeNode;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smackx.ServiceDiscoveryManager;
import org.jivesoftware.smackx.filetransfer.*;
public class FileSelectionMainGUI {


    ConnectionConfiguration configGoogle = new ConnectionConfiguration("jabber.org", 5222);

    JFrame mainWindow;
    JLabel selectEmailLabel,selectFileLabel;
    JButton browseFilesButton,sendButton;
    JTextField browseField;
    JComboBox buddyListBox;
    JPanel contentPanel;
    Roster roster;
    FileTransferManager fileTransferManager;
    XMPPConnection connection;
    DecimalFormat transferringProgress = new DecimalFormat("#");
    XMPPFileManager receiveManager;

    public void createGUI()
    {
        mainWindow = new JFrame("RECEIVER");
        mainWindow.setLayout(null);
        mainWindow.setSize(700, 600);
        mainWindow.setLocation(100, 50);

        contentPanel = new JPanel();
        contentPanel.setLayout(null);
        contentPanel.setBounds(0, 0, 700, 600);

        selectEmailLabel = new JLabel("Enter User Email ID");
        selectEmailLabel.setBounds(80, 325, 150, 30);
        contentPanel.add(selectEmailLabel);

        selectFileLabel = new JLabel("Select File");
        selectFileLabel.setBounds(96, 390, 150, 30);
        contentPanel.add(selectFileLabel);

        buddyListBox = new JComboBox();
        buddyListBox.addItem("hellobuddy.test2@gmail.com");
        buddyListBox.setBackground(Color.white);
        buddyListBox.setBounds(230, 325, 390, 30);
        contentPanel.add(buddyListBox);

        browseField = new JTextField();
        browseField.setText("underline.png");
        browseField.setBounds(230, 390, 220, 30);
        contentPanel.add(browseField);

        browseFilesButton = new JButton("Browse File");
        browseFilesButton.setBounds(470, 390, 130, 30);
        contentPanel.add(browseFilesButton);

        sendButton = new JButton("Send File");
        sendButton.setBounds(470, 490, 130, 30);
        contentPanel.add(sendButton);

        mainWindow.add(contentPanel);
        mainWindow.setVisible(true);

    }




    public void login(String userName,String password,String type_server)
        {


                    connection = new XMPPConnection(configGoogle);
                    XMPPConnection.DEBUG_ENABLED = true;
                    SASLAuthentication.supportSASLMechanism("PLAIN", 0);
                    try
                    {
                        connection.connect();
                        connection.login(userName, password);
                        System.out.println("logged in...");
                    }
                    catch(Exception e)
                    {

                        e.printStackTrace();
                    }
                    new ServiceDiscoveryManager(connection);
                                        fileTransferManager = new FileTransferManager(connection);

                                        FileTransferNegotiator.setServiceEnabled(connection, true);

                    fileTransferManager.addFileTransferListener(new FileTransferListener() {

                                        @Override
                                        public void fileTransferRequest(FileTransferRequest ftr) {
                                            System.out.println("got request");
                                       try {
                                            IncomingFileTransfer transfer = ftr.accept();
                                            System.out.println("status is:"+transfer.getStatus());

                                            transfer.recieveFile(new File("testdoc.txt"));
                                            System.out.println("file recieved");
                                        } catch (XMPPException ex) {
                                            Logger.getLogger(FileSelectionMainGUI.class.getName()).log(Level.SEVERE, null, ex);
                                            ex.printStackTrace();
                                        }
                                                                    }
                                        });


        }

    public static void main(String args[])
    {
        FileSelectionMainGUI obj = new FileSelectionMainGUI();
        obj.createGUI();
        obj.login("receiverjabberid", "password", "jabber");


    }

}

Оба успешно вошли в систему ... когда я нажимаю кнопку отправки от отправителя .. он ничего не делает без ошибок.просто подожди инфинте.Вы находите какие-либо проблемы с кодом?Я делаю что-то не так?

...