// 用户登录
public boolean loginByUserName(String userName, String passWord,
String flage) {
boolean result = false;
ConfigTools configTools = new ConfigTools();
ConnectionConfiguration config = new ConnectionConfiguration(configTools.getOpenfireIp());
// serverIp表示的是你openfire服务器地址
config.setReconnectionAllowed(false);
config.setSendPresence(false);
XMPPConnection connection = new XMPPConnection(config);
try {
connection.connect();
connection.login(userName, passWord, flage);
System.out.println(connection.getUser() + " 登录即时通讯服务器成功!");
result = true;
} catch (Exception e) {
System.out.println("登录即时通讯服务器失败!");
result = false;
}
return result;
}
// 用户退出
public boolean unLoginByUserName(String userName, String passWord) {
boolean result = false;
// serverIp表示的是你openfire服务器地址
XMPPConnection connection = ToolsUtil.getUserConn(userName, passWord);
try {
Presence presence = new Presence(Presence.Type.available);
connection.disconnect(presence);
System.out.println(connection.getUser() + "退出即时通讯服务器成功!");
result = true;
} catch (Exception e) {
System.out.println("退出即时通讯服务器失败!");
result = false;
}
return result;
}
// 发送消息
public void testSenderMsg(String jid, String body, String userName,
String passWord) {
try {
Message req = new Message();
req.setTo(jid);
req.setBody(body);
req.setProperty("name", "aaa");
req.setProperty("age", "123");
req.setProperty("bir", "2012-1-1");
XMPPConnection conn = ToolsUtil.getUserConn(userName, passWord);
conn.sendPacket(req);
} catch (RuntimeException e) {
e.printStackTrace();
}
}
// 添加用户
public void testAddUserMsg(String userName, String passWord) {
try {
XMPPConnection connection = ToolsUtil.getAdminConn();
try {
AccountManager accountManager = connection.getAccountManager();
Map attributes = new HashMap();
attributes.put("email", "email@sina.com");
attributes.put("name", "name");
attributes.put("dept", "部门");
accountManager.createAccount(userName, passWord, attributes);
} catch (XMPPException e) {
e.printStackTrace();
}
} catch (RuntimeException e) {
e.printStackTrace();
}
}
// 修改密码
public void testChangeUserMsg(String userName, String password,
String changePassword) {
try {
XMPPConnection connection = ToolsUtil.getUserConn(userName,
password);
try {
AccountManager accountManager = connection.getAccountManager();
accountManager.changePassword(changePassword);
} catch (XMPPException e) {
e.printStackTrace();
}
} catch (RuntimeException e) {
e.printStackTrace();
}
}
public static boolean deleteAccount(XMPPConnection connection) {
try {
connection.getAccountManager().deleteAccount();
return true;
} catch (Exception e) {
return false;
}
}
public static void getMsg(XMPPConnection connection) {
MessageListener msgListener = new MessageListener() {
public void processMessage(Chat chat, Message message) {
if (message != null && message.getBody() != null) {
System.out.println("收到消息:" + message.getBody());
}
}
};
Chat chat = connection.getChatManager()
.createChat("yancj", msgListener);
ChatManager manager = connection.getChatManager();
manager.addChatListener(new ChatManagerListener() {
public void chatCreated(Chat chat, boolean arg1) {
chat.addMessageListener(new MessageListener() {
public void processMessage(Chat arg0, Message message) {
// 若是聊天窗口已存在,将消息转往目前窗口
// 若是窗口不存在,开新的窗口并注册
}
});
}
});
}
public static void sendMessage(Chat chat, Message message)
throws XMPPException {
// Message msg = new Message();
// msg.setProperty("size", "size");
// msg.setBody(getSendInfo());//真正的消息
// chat.sendMessage(msg);
// chat.sendMessage(message);
}
public static void sendMessageCollector() {
System.out.println("&&&&&&&&&&&&");
try {
XMPPConnection connection = ToolsUtil
.getUserConn("zhangsan", "123");
PacketFilter filter = new AndFilter(new PacketTypeFilter(
Message.class), new FromContainsFilter(
"yancj@windows-4b7lu7t/web"));
PacketCollector myCollector = connection
.createPacketCollector(filter);
PacketListener myListener = new PacketListener() {
@Override
public void processPacket(Packet packet) {
System.out.println("____________________");
}
};
connection.addPacketListener(myListener, filter);
} catch (Exception e) {
}
}
public static VCard getUserVCard(XMPPConnection connection) throws XMPPException {
VCard vcard = new VCard();
vcard.load(connection);
System.out.println(vcard.getField("sex"));
System.out.println(vcard.getField("DESC"));
System.out.println(vcard.getEmailHome());
System.out.println(vcard.getOrganization());
System.out.println(vcard.getNickName());
System.out.println(vcard.getPhoneWork("PHONE"));
System.out.println(vcard.getProperty("DESC"));
System.out.println(vcard.getAvatar());
return vcard;
}
public static VCard getUserVCard(XMPPConnection connection, String userJid)
throws XMPPException {
VCard vcard = new VCard();
vcard.load(connection,userJid);
System.out.println(vcard.getOrganization());
System.out.println(vcard.getField("sex"));
System.out.println(vcard.getNickName());
System.out.println(vcard.getAvatar());
System.out.println(vcard.getField("DESC"));
return vcard;
}
public static void setUserVCard(XMPPConnection connection)
throws XMPPException {
VCard vCard = new VCard();
vCard.load(connection);
vCard.setEmailHome("lulu@sina.com");
vCard.setOrganization("售后");
vCard.setNickName("颜昌军t2");
vCard.setField("sex", "男");
vCard.setPhoneWork("PHONE", "3443233");
vCard.setField("DESC", "描述信息。。。");
// vCard.setAvatar(vCard.getAvatar());
vCard.save(connection);
System.out.println("添加成功");
}
public static ImageIcon getUserImage(XMPPConnection connection, String user) {
ImageIcon ic = null;
try {
System.out.println("获取用户头像信息: " + user);
VCard vcard = new VCard();
vcard.load(connection, user);
if (vcard == null || vcard.getAvatar() == null) {
return null;
}
ByteArrayInputStream bais = new ByteArrayInputStream(vcard
.getAvatar());
Image image = ImageIO.read(bais);
ic = new ImageIcon(image);
System.out.println("图片大小:" + ic.getIconHeight() + " "
+ ic.getIconWidth());
} catch (Exception e) {
e.printStackTrace();
}
return ic;
}
public static List searchUsers(XMPPConnection connection,
String serverDomain, String userName) throws XMPPException {
List results = new ArrayList();
System.out.println("查询开始..............." + connection.getHost()
+ connection.getServiceName());
UserSearchManager usm = new UserSearchManager(connection);
Form searchForm = usm.getSearchForm(serverDomain);
Form answerForm = searchForm.createAnswerForm();
answerForm.setAnswer("Username", true);
answerForm.setAnswer("search", userName);
ReportedData data = usm.getSearchResults(answerForm, serverDomain);
Iterator it = data.getRows();
Row row = null;
UserBean user = null;
while (it.hasNext()) {
user = new UserBean();
row = it.next();
user.setUserName(row.getValues("Username").next().toString());
user.setName(row.getValues("Name").next().toString());
user.setEmail(row.getValues("Email").next().toString());
System.out.println(row.getValues("Username").next());
System.out.println(row.getValues("Name").next());
System.out.println(row.getValues("Email").next());
results.add(user);
}
return results;
}
// public static void sendFile(String userName, String passWord, String
// user,
// File file){
// try {
// XMPPConnection connection = ToolsUtil.getUserConn(userName,
// passWord);
// System.out.println("发送文件开始" + file.getName());
// FileTransferManager transfer = new FileTransferManager(connection);
// OutgoingFileTransfer out =
// transfer.createOutgoingFileTransfer(user+"/Smack");
// out.sendFile(file, file.getName());
// // System.out.println("//");
// // System.out.println(out.getStatus());
// // System.out.println(out.getProgress());
// // System.out.println(out.isDone());
// // System.out.println("//");
// // System.out.println("发送文件结束");
// // long timeOut = 1000000;
// // long sleepMin = 3000;
// // long spTime = 0;
// // int rs = 0;
// // out.sendFile(file, file.getName());
// // rs = out.getStatus().compareTo(FileTransfer.Status.complete);
// // while(rs!=0){
// // rs = out.getStatus().compareTo(FileTransfer.Status.complete);
// // spTime = spTime + sleepMin;
// // if(spTime>timeOut){
// // return ;
// // }
// // Thread.sleep(sleepMin);
// // }
// System.out.println("//");
// System.out.println(out.getStatus());
// System.out.println(out.getProgress());
// System.out.println(out.isDone());
// System.out.println("//");
// System.out.println("发送文件结束");
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
public static void sendFile(String userName, String passWord, String user, File file) {
try {
XMPPConnection connection = ToolsUtil.getUserConn(userName,passWord);
System.out.println("发送文件开始" + file.getName());
FileTransferManager transfer = new FileTransferManager(connection);
String destination = user + "/spark";
final OutgoingFileTransfer out = transfer.createOutgoingFileTransfer(destination);
System.out.println(connection.getPort());
if (file.exists()) {
System.out.println("文件存在");
}
long timeOut = 100000;
long sleepMin = 3000;
long spTime = 0;
int rs = 0;
try {
byte[] fileData = getFileBytes(file);
OutputStream os = out.sendFile(file.getName(), fileData.length,"you won't like it");
os.write(fileData);
os.flush();
rs = out.getStatus().compareTo(FileTransfer.Status.complete);
while (rs != 0) {
System.out.println("getStatus" + out.getStatus().toString());
rs = out.getStatus().compareTo(FileTransfer.Status.complete);
System.out.println(rs);
spTime = spTime + sleepMin;
if (spTime > timeOut) {
System.out.println("fail" + "文件发送失败");
return;
}
Thread.sleep(sleepMin);
}
System.out.println("end send file");
} catch (XMPPException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static byte[] getFileBytes(File file) throws IOException {
BufferedInputStream bis = null;
byte[] buffer = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
int bytes = (int) file.length();
buffer = new byte[bytes];
int readBytes = bis.read(buffer);
if (readBytes != buffer.length) {
throw new IOException("not read");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
bis.close();
}
}
return buffer;
}
public static void main(String[] args) throws XMPPException {
UserMsg userMsg = new UserMsg();
// userMsg.testSenderMsg("test3@windows-4b7lu7t", "1111111111111", "yancj", "123");
// ConfigTools configTools = new ConfigTools();
XMPPConnection connection = ToolsUtil.getUserConn("yancjt2", "123");
// AccountManager accountManager = connection.getAccountManager();
// for (String attr : accountManager.getAccountAttributes()) {
// System.out.println(accountManager.getAccountAttribute("name"));
// }
// getUserImage(connection, "yancj1@linux65");
// ConfigTools configTools = new ConfigTools();
// searchUsers(connection, configTools.getOpenfireDomain(), "cte");
// userMsg.testAddUserMsg("test6", "123");
// try {
// } catch (Exception e) {
// e.printStackTrace();
// System.out.println(e.getMessage());
// }
// getUserVCard(connection,"admin@linux88");
// File file = new File("E:\\smarck.txt");
// sendFile("ctest1", "123", "ctest3@windows-4b7lu7t/web", file);
// setUserVCard(connection);
getUserVCard(connection);
}