Receive and send smack + openfire files

Source: Internet
Author: User

Reprint please explain the source: http://blog.csdn.net/steelychen/article/details/37958839

The User Name (for example, [email protected]) and local file full path (for example, E: \ XXX \ xxx.zip) must be provided for sending a file)

The received file must declare a file listener to listen for files that may be sent.

The client sends a file code snippet

Public void sendfire (xmppconnection Conn, string touser, string filepath) throws xmppexception {// conn must have been logged on. // register touser and only log on in one place, multiple logons may cause the primary node to fail to receive the request. filetransfermanager FTM = new filetransfermanager (conn ); /** check whether the passed user is correct * touser = "[email protected]" * 192.168.1.100 is the openfire server address */presence P = connection. getroster (). getpresence (touser); If (P = NULL) {system. out. println ("user does not exist"); return;} touser = P. getf Rom (); // extract the complete user name outgoingfiletransfer oft = FTM. createoutgoingfiletransfer (touser); // "Get my file" can be a random string, that is, an oft carrying information. sendfile (new file (filepath), "Get my file"); system. out. println ("Sending File status =" + oft. getstatus (); long starttime =-1; while (! Oft. isdone () {If (OFT. getstatus (). Equals (status. Error) {system. Out. println ("error !!! "+ Oft. geterror ();} else {double progress = oft. getprogress (); If (Progress> 0.0 & starttime =-1) {starttime = system. currenttimemillis ();} progress * = 100; system. out. println ("status =" + oft. getstatus (); system. out. println ("Progress =" + NF. format (Progress) + "%");} Try {thread. sleep (1000);} catch (interruptedexception e) {e. printstacktrace () ;}} system. out. println ("used" + (system. currenttimemillis ()-starttime)/1000) + "seconds ");}


The client receives file code snippets. the receiver's test program must note that the program cannot be exited during the test. Otherwise, the registered listener will be destroyed as the program exits. Of course, the user will also log out.

Simpledateformat SDF = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); Public void startrecvfilelisten (xmppconnection conn) {// conn must be a logged-on filetransfermanager = new filetransfermanager (conn); // Add a file receiving listener manager. addfiletransferlistener (New filetransferlistener () {// some methods are called every time a file is sent. Public void filetransferrequest (filetransferrequest request) {// The accetp that calls the request indicates receiving the file, you can also call the reject method to reject final in requests. Comingfiletransfer intransfer = request. accept (); try {system. out. println ("received File Sending request, file name:" + request. getfilename (); // Where to store the received file string filepath = "D: \ datas \ smackclient \" + request. getfilename (); intransfer. recievefile (new file (filepath); // if you want to obtain the status of received files from time to time, you must listen in the thread, if the current thread listens to the file status, the received file will be 0 New thread () {@ override public void run () {long starttime = system. currenttimemillis (); While (! Intransfer. isdone () {If (intransfer. getstatus (). equals (status. error) {system. out. println (SDF. format (new date () + "error !!! "+ Intransfer. geterror ();} else {double progress = intransfer. getprogress (); progress * = 100; system. out. println (SDF. format (new date () + "status =" + intransfer. getstatus (); system. out. println (SDF. format (new date () + "Progress =" + NF. format (Progress) + "%");} Try {thread. sleep (1000);} catch (interruptedexception e) {e. printstacktrace () ;}} system. out. println ("used" + (system. currenttimemillis ()-starttime)/1000) + "seconds ");}}. start ();} catch (xmppexception e) {joptionpane. showmessagedialog (null, "file failed", "error", joptionpane. error_message); E. printstacktrace () ;}}); system. out. println (connection. getuser () + "--" + connection. getservicename () + "Start listening for file transfer ");}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.