Xmpp 发送文件和接收文件 android

原帖:http://blog.csdn.net/wangyi_lin/article/details/6978458因为asmack有一些功能没有实现,其中包括传输文件的功能,所以要先修改asmack的源码。 如果不修改会出现这样一个包 [html] view plaincopy<streamhost jid="[email protected]/smack" host="127.0.0.1" port="7777"/> 服务器地址会被解析成127.0.0.1 导致这样一个错误。貌似smack 3.2.1 IBB 传输时有bug也会报这个错。 [html] view plaincopyCould not establish socket with any provided host 推荐使用强大的eclipse来编译和打包源码,只在要在源码路径下建一个工程,eclipse会帮你生成好目录,asmack依赖两个包,xmlpull和android的Apache包。后者不用理会,只需要吧xmlpull这个包加进去就可以了,不用理会报错,直接Export就行。只需要吧org.jivesoftware.smackx.filetransfer.Socks5TransferNegotiator.discoverLocalIP()改为 [java] view plaincopyprivate String discoverLocalIP() throws UnknownHostException { try { for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Logger.error("Error retrieving the local IP", ex); } throw new UnknownHostException("Failed to retrieve local IP"); //return InetAddress.getLocalHost().getHostAddress(); } 就可以了。 源代码中是这样的。。。。。。。非常坑爹 - -! [java] view plaincopyprivate String discoverLocalIP() throws UnknownHostException { return InetAddress.getLocalHost().getHostAddress(); } 英文原版网址:http://code.google.com/p/asmack/issues/detail?id=61准备工作做好以后,写代码就比较简单了。发送端:只需要注意 发送文件时所需的JID一定要是完整JID比如 [email protected]/smack[java] view plaincopyprivate static void sendFile(String user,XMPPConnection conn,File file) throws Exception{ FileTransferManager manager = new FileTransferManager(conn); OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(user); transfer.sendFile(file,"data backup"); manager.addFileTransferListener(new FileTransferListener() { public void fileTransferRequest(FileTransferRequest request) { try{ Log.e("Have file","file"); IncomingFileTransfer transfer = request.accept(); transfer.recieveFile(new File("complete_works.txt")); }catch(Exception e){ Log.e("RecFile Ex In!",e.getMessage()); } } }); } 接收端:ServiceDiscoverManager是一个很强大的类具体介绍看http://www.igniterealtime.org/builds/smack/docs/latest/javadoc/org/jivesoftware/smackx/ServiceDiscoveryManager.html[java] view plaincopyprivate static void setFileListener(final XMPPConnection conn) { Thread thread = new Thread() { public void run() { ServiceDiscoveryManager sdm = ServiceDiscoveryManager .getInstanceFor(conn); if (sdm == null) sdm = new ServiceDiscoveryManager(conn); sdm.addFeature("http://jabber.org/protocol/disco#info"); sdm.addFeature("jabber:iq:privacy"); // Create the file transfer manager final FileTransferManager managerListner = new FileTransferManager(conn); FileTransferNegotiator.setServiceEnabled(conn, true); Log.i("File transfere manager", "created"); // Create the listener managerListner.addFileTransferListener(new FileTransferListener() { @Override public void fileTransferRequest(final FileTransferRequest request) { Log.i("Recieve File","new file transfere request new file transfere request new file transfere request"); Log.i("file request","from" + request.getRequestor()); IncomingFileTransfer transfer = request.accept(); Log.i("Recieve File alert dialog", "accepted"); File file = new File("/sdcard/data/"+ request.getFileName()); try { if (!file.exists()){ Log.i("have no file",file.getPath()); file.createNewFile(); } transfer.recieveFile(file); }catch (Exception e) { e.printStackTrace(); } } }); } }; thread.start(); }

你可能感兴趣的:(android,XMPP)