java實現簡單XMPP發送消息和文件的簡單例子

  首先xmpp server需要安裝好配置並啓動,這裏只是實現簡單的文本發送和文件發送,接受需要用xmpp client來接收

下載地址:http://www.igniterealtime.org/downloads/index.jsp

   xmpp server(openfire)

   xmpp client(spark)

   Java XMPP client library(smack)

 

  smack下載完後,壓縮包裏面有例子、api、和jar(smack.jar、smackx.jar、smackx-debug.jar、smackx-jingle.jar)根據需求引用這些jar

 

下面是一個簡單的java代碼,這是做簡單測試,沒有詳細優化過(測試時發現個小問題:發文件需要對方加你爲好友後,你發文件的時候纔會顯示,不加好友的話發送時是接收方有提示但是沒看到發送過來的文件,如果是發生文本消息則沒有這個問題)

 

首先spark爲接收方登入

然後執行下面的java代碼

package com.test.xmpp;

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.FileTransferManager;
import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;

/**
 * @author test
 *
 */
public class TestXmpp {

    /**
     * @param args
     */
    public static void main(String[] args){
       
        String user = "user@testHost/spark";
        String host = "localhost";
        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);
         
         sendFile(user,getFile(),connection );
        // sendTextMessage(user,connection);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            connection.disconnect();
        }
       

    }
   
    public static File getFile(){
        File file = new File("D:/test.jpg");
        return file;
    }

//發送文件
    public static void sendFile(String user,File file,XMPPConnection connection )  throws Exception{
        FileTransferManager manager = new FileTransferManager(connection);
        OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(user);
        long timeOut = 1000000;
        long sleepMin = 3000;
        long spTime = 0;
        int rs = 0; 
      
            transfer.sendFile(file, "pls re 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);
            }

    }

//發送文本
    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("Hi Test Send Message........!");
    }
   
 

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章