Package test;
Import Java.io.File;
Import Org.jivesoftware.smack.Chat;
Import org.jivesoftware.smack.ConnectionConfiguration;
Import Org.jivesoftware.smack.MessageListener;
Import org.jivesoftware.smack.XMPPConnection;
Import org.jivesoftware.smack.XMPPException;
Import Org.jivesoftware.smack.packet.Message;
Import Org.jivesoftware.smackx.filetransfer.FileTransfer;
Import Org.jivesoftware.smackx.filetransfer.FileTransferListener;
Import Org.jivesoftware.smackx.filetransfer.FileTransferManager;
Import Org.jivesoftware.smackx.filetransfer.FileTransferRequest;
Import Org.jivesoftware.smackx.filetransfer.IncomingFileTransfer;
Import Org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;
/**
* Demo XMPP the ability to send incoming messages and files
*
* @author Wang
* @date 2010-1-14 16:59:24
*/
public class Xmpptest {
private static void Handletransferrequest (Filetransferrequest request) {
File File = new file (Request.getfilename ());
Incomingfiletransfer transfer = Request.accept ();
try {
Transfer.recievefile (file);
catch (Xmppexception e) {
E.printstacktrace ();
}
}
* * Send File/*
public static void SendFile (Filetransfermanager transfermanager,
String user, file file, xmppconnection connection) throws Exception {
Outgoingfiletransfer transfer = Transfermanager
. Createoutgoingfiletransfer (user);
Long timeOut = 100000;
Long sleepmin = 3000;
Long sptime = 0;
int rs = 0;
Transfer.sendfile (file, "Send file!");
rs = Transfer.getstatus (). CompareTo (FileTransfer.Status.complete);
while (Rs!= 0) {
rs = Transfer.getstatus (). CompareTo (FileTransfer.Status.complete);
SpTime = SpTime + sleepmin;
if (SpTime > TimeOut) {
Return
}
Thread.Sleep (sleepmin);
}
}
/* Send Text * *
public static void Sendtextmessage (String user, xmppconnection connection)
Throws Exception {
Chat Chat = Connection.getchatmanager (). Createchat (User,
New MessageListener () {
public void ProcessMessage (Chat Chat, message message) {
SYSTEM.OUT.PRINTLN ("Received message:" + message);
}
});
Chat.sendmessage ("Hello XMPP message!");
}
public static void Main (string[] args) {
String user = "Test1@test.com/spark";
String host = "192.168.33.73";
int port = 5222;
String username = "Test";
String password = "Test";
connectionconfiguration config = new Connectionconfiguration (host, Port);
Config.setcompressionenabled (TRUE);
Config.setsaslauthenticationenabled (TRUE);
Xmppconnection connection = new xmppconnection (config);
try {
Connection.connect ();
Connection.login (username, password);
/* Create Filetransfermanager object (after connect ()) * *
Filetransfermanager Transfermanager = new Filetransfermanager (
connection);
/* Add receive File monitor for Transfermanager * *
Transfermanager.addfiletransferlistener (New Filetransferlistener () {
public void Filetransferrequest (
Final Filetransferrequest request) {
Handletransferrequest (Request);
}
});
Sendtextmessage (user, connection);
SendFile (Transfermanager, User, new File ("D:/testuuid.java"),
connection);
Thread.Sleep (1000000);
catch (Exception e) {
E.printstacktrace ();
finally {
Connection.disconnect ();
}
}
}