Uploading file to ejabberd server using smack in android

How to upload file to ejabberd using smack

I am using:

  • ejabberd v22.10
    -operating system: windows 10
    -smack v4.2.0

This is my ejabberd.yml


hosts:
  - localhost

loglevel: debug


  
listen:
  -
    port: 5222
    ip: "::"
    module: ejabberd_c2s
    #starttls: true
    #certfile: "./xmpp_example_com.pem"
    

  -
    port: 5280
    ip: "::"
    module: ejabberd_http
    request_handlers:
      /admin: ejabberd_web_admin
  
  -
    port: 5281
    module: ejabberd_http
    ip: "::"
    request_handlers:
      /api: mod_http_api
      /oauth: ejabberd_oauth

  -
    port: 5443
    module: ejabberd_http
    request_handlers:
      /upload: mod_http_upload
    
  
  -
    port: 5347
    ip: "::"
    module: ejabberd_service
    hosts:
      "mymuc.domain.tld":
        password: "mysecret"

   
     
acl:
 exampleorg:
   server: localhost
    
api_permissions:
  "admin api permsions":
    from:
      - ejabberd_ctl
      - mod_http_api
    who:
      acl: exampleorg
    what: "*"
    




    
modules:
  mod_admin_extra: {}
  mod_muc:
    access:
      - allow
    access_admin:
      - allow: admin
    access_create: muc_create
    access_persistent: muc_create
    access_mam:
      - allow
    default_room_options:
      allow_subscription: true  # enable MucSub
      allow_private_messages: true
      allow_user_invites: true
      mam: true
      persistent: true
  mod_muc_admin: {}
  mod_push_keepalive: 
    wake_on_start: true
  mod_offline: {}
  mod_push: {}
  mod_http_upload:
    docroot: /ejabberd/upload
    put_url: "http://@HOST@:5443/upload"
  mod_roster: {}


  #mod_offline_notify: {}
   

    

This my java code

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jivesoftware.smackx.filetransfer.FileTransferListener;
import org.jivesoftware.smackx.filetransfer.FileTransferManager;
import org.jivesoftware.smackx.filetransfer.FileTransferRequest;
import org.jivesoftware.smackx.filetransfer.IncomingFileTransfer;
import org.jivesoftware.smackx.filetransfer.OutgoingFileTransfer;
import org.jivesoftware.smackx.httpfileupload.HttpFileUploadManager;
import org.jivesoftware.smackx.httpfileupload.element.Slot;
import org.jxmpp.jid.DomainBareJid;
import org.jxmpp.jid.impl.JidCreate;
import org.jxmpp.stringprep.XmppStringprepException;
import org.jxmpp.util.XmppStringUtils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyLoginTask task = new MyLoginTask();
        task.execute("");
    }

    private class MyLoginTask extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... params) {
            // Create a connection to the jabber.org server.
            XMPPTCPConnectionConfiguration conf = null;
            try {
                conf = XMPPTCPConnectionConfiguration.builder()
                        .setHostAddress(InetAddress.getByName("192.168.1.2"))
                        .setXmppDomain(JidCreate.domainBareFrom("localhost"))
                        .setUsernameAndPassword("admin", "asd")
                        .setPort(5222)
                        .setDebuggerEnabled(true)
                        .setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
                        .build();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (XmppStringprepException e) {
                e.printStackTrace();
            }
            AbstractXMPPConnection conn1= new XMPPTCPConnection(conf);


            try {
                conn1.connect();
                if(conn1.isConnected()) {
                    Log.w("app", "conn done");
                }
                conn1.login();

                if(conn1.isAuthenticated()) {
                    Log.w("app", "Auth done");
                /*    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new
                            File(getFilesDir()+File.separator+"MyFile.txt")));
                    bufferedWriter.write("dsdsa dsadsasa");
                    bufferedWriter.close();
                    Log.w("file_sent",new File(getFilesDir()+File.separator+"MyFile.txt").length()+"");
                    FileTransferManager manager = FileTransferManager.getInstanceFor(conn1);
                    OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer("admin@localhost/Smack");
                    transfer.sendFile(new File(getFilesDir()+File.separator+"MyFile.txt"), "You won't believe this!");
               // receive file

                    if(manager==null) {
                        manager = FileTransferManager.getInstanceFor(conn1);
                    }
                   manager.addFileTransferListener(new FileTransferListener() {
                        @Override
                        public void fileTransferRequest(FileTransferRequest request) {
                            IncomingFileTransfer transfer = request.accept();

                            try {
                                Log.w("7elo", "7elo");
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(getApplicationContext(), "File is received",
                                                Toast.LENGTH_LONG).show();
                                    }
                                });

                               File file = new File(getFilesDir()+File.separator+"receivedFile.txt");
                               transfer.recieveFile(file);
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        final Handler handler = new Handler();
                                        handler.postDelayed(new Runnable() {
                                            @Override
                                            public void run() {
                                                // Do something after 5s = 5000ms
                                                Log.w("file_received",file.length()+"");
                                            }
                                        }, 1000*60);
                                    }
                                });

                            } catch (SmackException | IOException e) {
                                e.printStackTrace();

                            }
                        }
                    });
                  */
                    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new
                            File(getFilesDir()+File.separator+"MyFile.txt")));
                    bufferedWriter.write("upload is success");
                    bufferedWriter.close();
                    //  HttpFileUploadManager manager = HttpFileUploadManager.getInstanceFor(conn1);
                    try {
                        HttpFileUploadManager httpFileUploadManager =HttpFileUploadManager.getInstanceFor(conn1);
                        httpFileUploadManager.discoverUploadService();
                        httpFileUploadManager.uploadFile(new File(getFilesDir()+File.separator+"MyFile.txt"));

                    } catch (Exception e) {

                        //handle failure
                    }


                }
            }
            catch (Exception e) {
                Log.w("app", e.toString());
            }

            return "";
        }


        @Override
        protected void onPostExecute(String result) {
        }




    }

}

I am getting this error

W/ServiceDiscoveryManager: Could not discover information about service
    org.jivesoftware.smack.XMPPException$XMPPErrorException: XMPP error reply received from localhost: XMPPError: service-unavailable - cancel
        at org.jivesoftware.smack.StanzaCollector.nextResultOrThrow(StanzaCollector.java:256)
        at org.jivesoftware.smack.StanzaCollector.nextResultOrThrow(StanzaCollector.java:208)
        at org.jivesoftware.smackx.disco.ServiceDiscoveryManager.discoverInfo(ServiceDiscoveryManager.java:540)
        at org.jivesoftware.smackx.disco.ServiceDiscoveryManager.discoverInfo(ServiceDiscoveryManager.java:506)
        at org.jivesoftware.smackx.disco.ServiceDiscoveryManager.findServicesDiscoverInfo(ServiceDiscoveryManager.java:743)
        at org.jivesoftware.smackx.httpfileupload.HttpFileUploadManager.discoverUploadService(HttpFileUploadManager.java:175)
        at org.jivesoftware.smackx.httpfileupload.HttpFileUploadManager$2.authenticated(HttpFileUploadManager.java:114)
        at org.jivesoftware.smack.AbstractXMPPConnection.callConnectionAuthenticatedListener(AbstractXMPPConnection.java:1228)
        at org.jivesoftware.smack.AbstractXMPPConnection.afterSuccessfulLogin(AbstractXMPPConnection.java:572)
        at org.jivesoftware.smack.tcp.XMPPTCPConnection.afterSuccessfulLogin(XMPPTCPConnection.java:377)
        at org.jivesoftware.smack.tcp.XMPPTCPConnection.loginInternal(XMPPTCPConnection.java:442)
        at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:491)
        at org.jivesoftware.smack.AbstractXMPPConnection.login(AbstractXMPPConnection.java:448)
        at com.example.myapplication.MainActivity$MyLoginTask.doInBackground(MainActivity.java:79)
        at com.example.myapplication.MainActivity$MyLoginTask.doInBackground(MainActivity.java:52)
        at android.os.AsyncTask$2.call(AsyncTask.java:304)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
        at java.lang.Thread.run(Thread.java:762)