1. 修改自身状态
包括上线,隐身,对某人隐身,对某人上线
- public static void updateStateToAvailable(XMPPConnection connection)
- {
- Presence presence = new Presence(Presence.Type.available);
- connection.sendPacket(presence);
- }
-
- public static void updateStateToUnAvailable(XMPPConnection connection)
- {
- Presence presence = new Presence(Presence.Type.unavailable);
- connection.sendPacket(presence);
- }
-
- public static void updateStateToUnAvailableToSomeone(XMPPConnection connection,String userName)
- {
- Presence presence = new Presence(Presence.Type.unavailable);
- presence.setTo(userName);
- connection.sendPacket(presence);
- }
- public static void updateStateToAvailableToSomeone(XMPPConnection connection,String userName)
- {
- Presence presence = new Presence(Presence.Type.available);
- presence.setTo(userName);
- connection.sendPacket(presence);
-
- }
2. 心情修改
-
-
-
-
-
- public static void changeStateMessage(XMPPConnection connection,String status)
- {
- Presence presence = new Presence(Presence.Type.available);
- presence.setStatus(status);
- connection.sendPacket(presence);
-
- }
3. 修改用户头像
有点麻烦,主要是读入图片文件,编码,传输之
- public static void changeImage(XMPPConnection connection,File f) throws XMPPException, IOException
- {
-
- VCard vcard = new VCard();
- vcard.load(connection);
-
- byte[] bytes;
-
- bytes = getFileBytes(f);
- String encodedImage = StringUtils.encodeBase64(bytes);
- vcard.setAvatar(bytes, encodedImage);
- vcard.setEncodedImage(encodedImage);
- vcard.setField("PHOTO", "image/jpg"
- + encodedImage + "", true);
-
-
- ByteArrayInputStream bais = new ByteArrayInputStream(
- vcard.getAvatar());
- Image image = ImageIO.read(bais);
- ImageIcon ic = new ImageIcon(image);
-
-
-
- vcard.save(connection);
-
- }
-
- private static byte[] getFileBytes(File file) throws IOException {
- BufferedInputStream bis = null;
- try {
- bis = new BufferedInputStream(new FileInputStream(file));
- int bytes = (int) file.length();
- byte[] buffer = new byte[bytes];
- int readBytes = bis.read(buffer);
- if (readBytes != buffer.length) {
- throw new IOException("Entire file not read");
- }
- return buffer;
- } finally {
- if (bis != null) {
- bis.close();
- }
- }
- }
4. 补充,用户状态的监听
即对方改变头像,状态,心情时,更新自己用户列表,其实这里已经有smack实现的监听器
- final Roster roster = Client.getRoster();
-
- roster.addRosterListener(
- new RosterListener() {
-
- @Override
- public void entriesAdded(Collection arg0) {
-
- System.out.println("--------EE:"+"entriesAdded");
- }
-
- @Override
- public void entriesDeleted(Collection arg0) {
-
- System.out.println("--------EE:"+"entriesDeleted");
- }
-
- @Override
- public void entriesUpdated(Collection arg0) {
-
- System.out.println("--------EE:"+"entriesUpdated");
- }
-
- @Override
- public void presenceChanged(Presence arg0) {
-
- System.out.println("--------EE:"+"presenceChanged");
- }
-
- });
-
三天时间,赶在最后一下午实现了文件的传输,本来需要实现离线文件的发送的,一直没想好怎么弄,找openfire的离线文件插件没找到,后来想出
一种方法,起服务器时起了一个系统用户,一直在线,当用户发送离线文件,检测到对方不存在,先发给系统用户,存到服务器路径,并在数据库中保存信息,当对
方上线时,系统用户查表,拿文件发送
想是这么想的,问题是时间太紧,没有实现,囧。
下一篇写离线消息和离线文件
1. 文件的发送
开一个文件选择框,选中文件后再调用下面的方法
- public static void sendFile(XMPPConnection connection,
- String user, File file) throws XMPPException, InterruptedException {
-
- System.out.println("发送文件开始"+file.getName());
- FileTransferManager transfer = new FileTransferManager(Client.getConnection());
- System.out.println("发送文件给: "+user+Client.getServiceNameWithPre());
- OutgoingFileTransfer out = transfer.createOutgoingFileTransfer(user+Client.getServiceNameWithPre()+"/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("发送文件结束");
- }
2. 文件接收,必须使用监听
晕死,在演示的时候竟然发送文件崩盘了。。。。。实在无语
对了,在发送文件的createOutgoing那边有问题,貌似/Smack,哎,对spark发送就不成功
阅读(2479) | 评论(0) | 转发(0) |