Java implementation XMPP send receive messages and file functions __xmpp

Source: Internet
Author: User
Tags sendfile

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 ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.