Realization of File transfer function of instant chat system based on smack

Source: Internet
Author: User

The function implementation process is:

1. File Sender User Login

2. Determine the file's recipient and file path to be sent

3. Get the connection

4. Obtain the full jid of the file receiver according to the connection (e.g. [email protected]/miniqq 2.3.6), where miniqq 2.3.6 is the resource of the user client of the file receiver, must obtain, otherwise the file cannot be sent

5. Sending files


The implementation code is:

<span style= "FONT-SIZE:18PX;" >public int Transfile (string touser, String filedir) {//final int flag[] = new int[] {0}; Connection conn = this.connection; Servicediscoverymanager Servicediscoverymanager = new Servicediscoverymanager (conn);//System.out.println ( Conn.tostring ());//System.out.println (This.sessionManager.getConnection (). toString ()); if (!touser.contains ("@") {Touser = Touser + "@" + this.connection.getServiceName ();} Touser = Touser + "/spark 2.6.3"; ROSTER roster = Connection.getroster ();  Presence Pre = Roster.getpresence (touser), Touser = Pre.getfrom (), if (!touser.contains ("/")) {//touser = Touser + "/MiniQQ 2.3.6 "; touser = Touser +"/miniqq 2.3.6 ";} SYSTEM.OUT.PRINTLN ("Send file:" + touser);//System.out.println (Touser); if (pre = = null) {System.out.println ("user does not exist"); return-1;} Filetransfermanager Transfermanager = new Filetransfermanager (conn); final file File = new file (Filedir); final Outgoingfiletransfer transfer = Transfermanager.createoutgoingfiletransfer (Touser);//Futuretask<integer> task = new futuretask<integer> (); futuretask<integer> task = new futuretask<> (new callable<integer> () {@Overridepublic Integer call () Throws Exception {int flag = 0;try {transfer.sendfile (file, "sending"), while (!transfer.isdone ()) {try {thread.sleep (10L );} catch (Interruptedexception e) {e.printstacktrace ();} if (transfer.getstatus () = = FileTransfer.Status.in_progress) {System.out.println (transfer.getprogress ());//flag[0] = 0;} else {if (transfer.getstatus () = = FileTransfer.Status.error) {System.out.println ("File transport Failed");////Flag[0] = -1;flag = -1;//return;} if (transfer.getstatus () = = FileTransfer.Status.refused) {System.out.println ("File transport was refused");//flag[0] = -1;flag = -1;//return;}}} catch (Xmppexception E1) {e1.printstacktrace ();//return;} return flag;}); Swingutilities.invokelater (Task), int flagres = 0;while (True) {if (Task.isdone ()) {try {flagres = Task.get ();} catch (Inte Rruptedexception e) {E.prinTstacktrace ();} catch (Executionexception e) {e.printstacktrace ();} Break;}} return flagres;} </span>


Realization of File transfer function of instant chat system based on smack

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.