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