您好我正在尝试发送文件。但它不会启动传输文件。在检查时我发现收件人监听器没有收到发件人的请求。 这是发件人代码:( ruff gui codes ..只看文件传输的东西)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");
}
}
两者都成功登录... 当我按发件人的发送按钮时...它没有任何错误。只是等待infinte。你发现代码有什么问题吗?我做错了什么?