Realization of file receiving function of instant chat system based on smack

Source: Internet
Author: User

The realization idea is:

1. User login to the receiving party

2. Get the file sender user name

3. Get the file name you want to receive

4. Get the file size you want to receive

5. Set the file storage path

6. Receiving files

The implementation code is:

public int Settransfilecallback (final Method method,final Object instance, final String Downloadedir) { Filetransfermanager Transfermanager = new Filetransfermanager (this.connection); Transfermanager.addfiletransferlistener (New Filetransferlistener () {public void Filetransferrequest (final Filetransferrequest request) {String Fromwhere = Request.getrequestor (); int pos = Fromwhere.lastindexof (' @ '); String from = fromwhere.substring (0, POS), if (!from.equals ("Sharefilemanager")) Swingutilities.invokelater (new Runnable () {public void run () {String fileName = Request.getfilename (); Long fileSize = Request.getfilesize (); String requestor = Request.getrequestor (); String Barejid = org.jivesoftware.smack.util.StringUtils.parseBareAddress (requestor); Byteformat format = new Byteformat (); String Text = Format.format (fileSize); Incomingfiletransfer transfer = Request.accept (); File Filecreate = new file (Downloadedir), if (!filecreate.exists ()) {filecreate.mkdirs ();} File Downloadedfile = new file (Downloadedir,Request.getfilename ()); try {transfer.recievefile (downloadedfile); while (true) {if (transfer.getstatus () = = FileTransfer.Status.complete) {try {Method.invoke (instance,new object[] {filename,barejid, text,downloadedir});} catch (IllegalArgumentException e) {e.printstacktrace ();} catch (Illegalaccessexception e) {e.printstacktrace ();} catch (InvocationTargetException e) {e.printstacktrace ();} return;} try {thread.sleep (10L);} catch (Interruptedexception e) {e.printstacktrace ();}}} catch (Xmppexception e) {e.printstacktrace ();}}});}); return 0;}


Realization of file receiving 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.