B4J Tutorial [Server] Upload files from your B4A app to your B4J server over the internet

Discussion in 'B4J Tutorials' started by Erel, Jan 28, 2014.

Thread Status:
Not open for further replies.
  1. Erel

    Erel Administrator Staff Member Licensed User

    This example shows how to make your B4J server application accessible over the internet.

    The implemented server is a simple file server that allows you to upload files from your B4A application to the server over the internet.

    The following steps are required in order to make your server accessible over the internet:
    1. As the router (or computer) external IP address changes from time to time we need to use a service that maps a known address to our dynamic address. In this case I used a free service named DuckDNS. You should register with them and choose a unique domain for your server.
      They will also provide you with a token. In the Main module we use a timer to call their service (with this token) every 10 minutes. This updates the IP address if needed.
    2. Router setup
      • Port forwarding - Configure the router to forward all incoming requests on the given port to your computer address.
      • Static IP - On some routers port forwarding will only work with a specific local IP address. The solution is to configure the router to always provide your computer with the same address.
      • Search Google for instructions specific to your router.
    3. Firewall - You need to allow incoming connections on the specific port.
    - There are several points of failures in this configuration. In order to debug it, run the server and call the test handler. Start with calling it from the computer browser ( If there is an error then the server is not running properly.
    Now try to call it with duckdns: yourdomain.duckdns.org:54021/test
    If it works then the configuration is probably fine. You just need to test it from outside the local network.
    If it doesn't work then find your external ip address and try to call it. If it doesn't work with the external ip address then your router is not configured correctly or the firewall is blocking the incoming connection.

    - Don't worry. Eventually you will get it working :)

    Now for the actual file server. The handler is quite simple. It supports two types of messages: text messages and files. The handler reads the data directly from the request input stream. The type parameter (from the query string) tells it whether to treat the data as a text message or as a file that needs to be saved.

    One important point is that we need to call req.InputStream before calling req.GetParameter. Otherwise the complete stream will be internally read as the server looks for the parameter in the request body. This will not happen if we first call req.InputStream.

    The Android application is quite simple. It just uses HttpUtils2 to send the file or text message.


    In order to run it you need to update the link variable based on your domain name.

    This tutorial opens the door to many interesting solutions. You can easily turn any PC into a backend server for your Android applications.

    Attached Files:

  2. billzhan

    billzhan Active Member Licensed User

    I am using b4a 2.71, when I compile and run FileClient-B4A (debug and release), the error occur:

    Parsing code.                          0.00
    Compiling code.                        Error
    After I create a new layout file with Ver.2.71 and replace the old one.It is works fine with the file server.

    Attached Files:

  3. derez

    derez Expert Licensed User

    Can it be modified to download files as well ?
    Rockefeller Goldman likes this.
  4. Erel

    Erel Administrator Staff Member Licensed User

    @billzhan layout files created with v3.20 will not work with previous versions.

    @derez sure. Create a new Download handler. Open an InputStream and use File.Copy2 to copy it to the response OutputStream.
    Rockefeller Goldman likes this.
  5. derez

    derez Expert Licensed User

    The Server file claims it is of a newer version and does not load the utils modules. I downloaded the last version of b4j (1.06) but still :(
    BillMeyer likes this.
  6. Erel

    Erel Administrator Staff Member Licensed User

    wyatt420 likes this.
  7. Erel

    Erel Administrator Staff Member Licensed User

    It is actually simpler than I wrote. The server handles "static" files automatically. You just need to put the files under the static files folder and they will be accessible. You do not need a custom handler for that.
  8. derez

    derez Expert Licensed User

    It works within my lan with the duckdns.
    I sent messages, b4a file and a picture. The picture arrived without the ".jpg" but after adding it it opens.:)
    Now I'll try to add the downloading side.
  9. Erel

    Erel Administrator Staff Member Licensed User

    It should work outside of the lan as well. Turn off the wifi in your phone to test it.

    The file name depends on how you fetched it from the device. The gallery for example will not return the full name. There is code in the forum that tries to resolve the content provider name to the actual file.
  10. derez

    derez Expert Licensed User

    I don't understand.
  11. Erel

    Erel Administrator Staff Member Licensed User

    See this simple example: Building web servers with B4J

    It is an example of a website. Not related to B4A.

    The html files and images (from the www folder) in this example are served automatically to the browser.
    Any file that is under a subfolder of the static files folder can be accessed (downloaded) automatically.

    For example if there is a file named database.db under www folder then you can download it by pointing the browser to:

    In the same way you can call Job.Download(<host>/database.db).
  12. derez

    derez Expert Licensed User

    Thank you it is clear now.
  13. derez

    derez Expert Licensed User

    I added this line
    srvr.StaticFilesFolder = File.Combine(File.DirApp, "www")
    to AppStart, put a file there and it works.:)

    Example for a use: I put some of my applications apk files in the server's www directory. From a device I put the domain and port in the browser and download the file that I want, go to file manager/downloads and install the application !
    Last edited: Jan 29, 2014
    Erel likes this.
  14. derez

    derez Expert Licensed User

    How can I design the static files folder, like add some welcome sentence ?
  15. Erel

    Erel Administrator Staff Member Licensed User

    It is simple. However it is better if you can start a new thread for this question.
  16. derez

    derez Expert Licensed User

    I activated the server from the console.:rolleyes:
    I found out that if you activate it not from the same folder - the uploaded folder is created at the launch folder.
    So this is the correct way, copy from the console, with a log for uploaded picture:
    Last edited: Jan 31, 2014
  17. sulsys

    sulsys Member Licensed User

    Worked like a charm first try. Knew the file I wanted to upload so removed the cc. Powerful and fast.

    Thank you for all the "usable" code. So many times the samples I see are theoretical but yours are useful. Keep up the good work.
  18. NinJavier

    NinJavier Member Licensed User

    to run the server module .exe or .jar the following error occurs: A Java Exception has ocurred.
  19. Erel

    Erel Administrator Staff Member Licensed User

    I guess that it works when you run it from the IDE, right?

    Make sure that #MergeLibraries is set to True.
    NinJavier likes this.
  20. luke2012

    luke2012 Well-Known Member Licensed User

    Very useful tutorial @Erel!
    The only "limitation" about this solution regarding the configuration that enable the Server to be available over the internet.
    I mean that the Server setup cannot be done by the final user (if the user doesn't have an internal IT person).

    So we can distribute the client (android app) and the Server with the server app but we have to specify the above instructions to the final user.
Thread Status:
Not open for further replies.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice