XMPP——Smack[5]文件传输及离线消息的获取

三天时间,赶在最后一下午实现了文件的传输,本来需要实现离线文件的发送的,一直没想好怎么弄,找openfire的离线文件插件没找到,后来想出一种方法,起服务器时起了一个系统用户,一直在线,当用户发送离线文件,检测到对方不存在,先发给系统用户,存到服务器路径,并在数据库中保存信息,当对方上线时,系统用户查表,拿文件发送

想是这么想的,问题是时间太紧,没有实现,囧。

下一篇写离线消息和离线文件

1. 文件的发送

开一个文件选择框,选中文件后再调用下面的方法

[java] view plain copy print ?
  1. public static void sendFile(XMPPConnection connection,
  2. String user, File file) throws XMPPException, InterruptedException {
  3. System.out.println("发送文件开始"+file.getName());
  4. FileTransferManager transfer = new FileTransferManager(Client.getConnection());
  5. System.out.println("发送文件给: "+user+Client.getServiceNameWithPre());
  6. OutgoingFileTransfer out = transfer.createOutgoingFileTransfer(user+Client.getServiceNameWithPre()+"/Smack");//
  7. out.sendFile(file, file.getName());
  8. System.out.println("//////////");
  9. System.out.println(out.getStatus());
  10. System.out.println(out.getProgress());
  11. System.out.println(out.isDone());
  12. System.out.println("//////////");
  13. System.out.println("发送文件结束");
  14. }
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. 文件接收,必须使用监听

[java] view plain copy print ?
  1. FileTransferManager transfer = new FileTransferManager(connection);
  2. transfer.addFileTransferListener(new RecFileTransferListener());
  3. public class RecFileTransferListener implements FileTransferListener {
  4. public String getFileType(String fileFullName)
  5. {
  6. if(fileFullName.contains("."))
  7. {
  8. return "."+fileFullName.split("//.")[1];
  9. }else{
  10. return fileFullName;
  11. }
  12. }
  13. @Override
  14. public void fileTransferRequest(FileTransferRequest request) {
  15. System.out.println("接收文件开始.....");
  16. final IncomingFileTransfer inTransfer = request.accept();
  17. final String fileName = request.getFileName();
  18. long length = request.getFileSize();
  19. final String fromUser = request.getRequestor().split("/")[0];
  20. System.out.println("文件大小:"+length + " "+request.getRequestor());
  21. System.out.println(""+request.getMimeType());
  22. try {
  23. JFileChooser chooser = new JFileChooser();
  24. chooser.setCurrentDirectory(new File("."));
  25. int result = chooser.showOpenDialog(null);
  26. if(result==JFileChooser.APPROVE_OPTION)
  27. {
  28. final File file = chooser.getSelectedFile();
  29. System.out.println(file.getAbsolutePath());
  30. new Thread(){
  31. public void run()
  32. {
  33. try {
  34. System.out.println("接受文件: " + fileName);
  35. inTransfer
  36. .recieveFile(new File(file
  37. .getAbsolutePath()
  38. + getFileType(fileName)));
  39. Message message = new Message();
  40. message.setFrom(fromUser);
  41. message.setProperty("REC_SIGN", "SUCCESS");
  42. message.setBody("["+fromUser+"]发送文件: "+fileName+"/r/n"+"存储位置: "+file.getAbsolutePath()+ getFileType(fileName));
  43. if (Client.isChatExist(fromUser)) {
  44. Client.getChatRoom(fromUser).messageReceiveHandler(
  45. message);
  46. } else {
  47. ChatFrameThread cft = new ChatFrameThread(
  48. fromUser, message);
  49. cft.start();
  50. }
  51. } catch (Exception e2) {
  52. e2.printStackTrace();
  53. }
  54. }
  55. }.start();
  56. }else{
  57. System.out.println("拒绝接受文件: "+fileName);
  58. request.reject();
  59. Message message = new Message();
  60. message.setFrom(fromUser);
  61. message.setBody("拒绝"+fromUser+"发送文件: "+fileName);
  62. message.setProperty("REC_SIGN", "REJECT");
  63. if (Client.isChatExist(fromUser)) {
  64. Client.getChatRoom(fromUser)
  65. .messageReceiveHandler(message);
  66. } else {
  67. ChatFrameThread cft = new ChatFrameThread(
  68. fromUser, message);
  69. cft.start();
  70. }
  71. }
  72. /* InputStream in = inTransfer.recieveFile();
  73. String fileName = "r"+inTransfer.getFileName();
  74. OutputStream out = new FileOutputStream(new File("d:/receive/"+fileName));
  75. byte[] b = new byte[512];
  76. while(in.read(b) != -1)
  77. {
  78. out.write(b);
  79. out.flush();
  80. }
  81. in.close();
  82. out.close();*/
  83. } catch (Exception e) {
  84. e.printStackTrace();
  85. }
  86. System.out.println("接收文件结束.....");
  87. }
  88. }
FileTransferManager transfer = new FileTransferManager(connection);transfer.addFileTransferListener(new RecFileTransferListener());public class RecFileTransferListener implements FileTransferListener { public String getFileType(String fileFullName) { if(fileFullName.contains(".")) { return "."+fileFullName.split("//.")[1]; }else{ return fileFullName; } } @Override public void fileTransferRequest(FileTransferRequest request) { System.out.println("接收文件开始....."); final IncomingFileTransfer inTransfer = request.accept(); final String fileName = request.getFileName(); long length = request.getFileSize(); final String fromUser = request.getRequestor().split("/")[0]; System.out.println("文件大小:"+length + " "+request.getRequestor()); System.out.println(""+request.getMimeType()); try { JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File(".")); int result = chooser.showOpenDialog(null); if(result==JFileChooser.APPROVE_OPTION) { final File file = chooser.getSelectedFile(); System.out.println(file.getAbsolutePath()); new Thread(){ public void run() { try { System.out.println("接受文件: " + fileName); inTransfer .recieveFile(new File(file .getAbsolutePath() + getFileType(fileName))); Message message = new Message(); message.setFrom(fromUser); message.setProperty("REC_SIGN", "SUCCESS"); message.setBody("["+fromUser+"]发送文件: "+fileName+"/r/n"+"存储位置: "+file.getAbsolutePath()+ getFileType(fileName)); if (Client.isChatExist(fromUser)) { Client.getChatRoom(fromUser).messageReceiveHandler( message); } else { ChatFrameThread cft = new ChatFrameThread( fromUser, message); cft.start(); } } catch (Exception e2) { e2.printStackTrace(); } } }.start(); }else{ System.out.println("拒绝接受文件: "+fileName); request.reject(); Message message = new Message(); message.setFrom(fromUser); message.setBody("拒绝"+fromUser+"发送文件: "+fileName); message.setProperty("REC_SIGN", "REJECT"); if (Client.isChatExist(fromUser)) { Client.getChatRoom(fromUser) .messageReceiveHandler(message); } else { ChatFrameThread cft = new ChatFrameThread( fromUser, message); cft.start(); } } /* InputStream in = inTransfer.recieveFile(); String fileName = "r"+inTransfer.getFileName(); OutputStream out = new FileOutputStream(new File("d:/receive/"+fileName)); byte[] b = new byte[512]; while(in.read(b) != -1) { out.write(b); out.flush(); } in.close(); out.close();*/ } catch (Exception e) { e.printStackTrace(); } System.out.println("接收文件结束....."); }}

晕死,在演示的时候竟然发送文件崩盘了。。。。。实在无语

对了,在发送文件的createOutgoing那边有问题,貌似/Smack,哎,对spark发送就不成功

短短三天,查资料差得头晕,中文的信息貌似少之又少,哎,匆匆完成,只能算是个半成品,大家自己完善吧。

呵呵,下一篇最后一篇了,谢谢离线消息和离线文件吧

你可能感兴趣的:(smack,openfire,XMPP,IM)