三天时间,赶在最后一下午实现了文件的传输,本来需要实现离线文件的发送的,一直没想好怎么弄,找openfire的离线文件插件没找到,后来想出一种方法,起服务器时起了一个系统用户,一直在线,当用户发送离线文件,检测到对方不存在,先发给系统用户,存到服务器路径,并在数据库中保存信息,当对方上线时,系统用户查表,拿文件发送
想是这么想的,问题是时间太紧,没有实现,囧。
下一篇写离线消息和离线文件
- 文件的发送
开一个文件选择框,选中文件后再调用下面的方法
[java] view plaincopyprint?
- 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("发送文件结束");
- }
- 文件接收,必须使用监听
[java] view plaincopyprint?
- 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发送就不成功
短短三天,查资料差得头晕,中文的信息貌似少之又少,哎,匆匆完成,只能算是个半成品,大家自己完善吧。
呵呵,下一篇最后一篇了,谢谢离线消息和离线文件吧
XMPP——Smack[5]文件传输及离线消息的获取,布布扣,bubuko.com
时间: 2024-10-25 11:28:13