B4A Library Android SFTP based on JSch tutorial

Discussion in 'Additional libraries, classes and official updates' started by Erel, Mar 5, 2013.

  1. Erel

    Erel Administrator Staff Member Licensed User

    JSch is an open source Java implementation of SSH2. It supports many features.

    Currently the Basic4android JSch library supports the SFTP protocol which is SSH File Transfer Protocol or Secured File Transfer Protocol.

    SFTP is similar to FTP with the difference that the communication is done over a secured channel.

    A good tutorial about SSH authentication is available here: SSH Host Key Protection | Symantec Connect Community

    When the client first connects to the SSH server he needs to approve the host key (unless it is in the list of approved keys).


    The SFtp object raises a PromptYesNo event for this question:
    Sub sftp1_PromptYesNo (Message As String)
    Dim res As Int = Msgbox2(Message, """Yes""""No"Null)
    'The next line might be a bit confusing. It is a condition.
       'The value will be True if res equals to DialogResponse.POSITIVE.
       sftp1.SetPromptResult(res = DialogResponse.POSITIVE)
    End Sub
    The network thread will wait until you call SetPromptResult or after 60 seconds. In the later case the result will be False.

    If you want to automatically accept the host key (which should only be done in a secured local network) then you can write:
    Sub sftp1_PromptYesNo (Message As String)
    End Sub
    Sftp should be declared as a process global variable and initialized in Activity_Create (or Service_Create):
    Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
    File.DirInternal, "hosts.txt")
    End If
    End Sub
    Calling SetKnownHostsStore sets a file that will save the known keys. Without it the user will need to approve the key each time.

    In Activity_Resume we need to call SFtp.Activity_Resume:
    Sub Activity_Resume
    End Sub
    The purpose of this call is to redisplay the visible prompt if it was visible when the activity was paused. This is relevant for example when the user changes the orientation while the dialog is visible.

    SFtp can also show a message to the user in some cases. This is done with the ShowMessage event:
    Sub sftp1_ShowMessage (Message As String)
    Msgbox(Message, "")
    End Sub
    The other methods work in the same way as the FTP object: Android FTP tutorial

    The library is attached. The open source project is embedded in the jar file.

    V1.30 - Based on the latest version of Jsch (0.1.54).

    Attached Files:

    Last edited: Nov 2, 2016
    Merlot2309, balarii, AscySoft and 5 others like this.
  2. AscySoft

    AscySoft Active Member Licensed User

    nicely done

    Well, and finally him does the job...again, I mean, what do you want more...??
    A big thanks, now I feel more secure!
    And maybe one day(or two) somebody will develop another fine library supporting FTPS (see Apache.network.common)
    So, to you Erel a BIG THANKS!
  3. Erel

    Erel Administrator Staff Member Licensed User

    Thank you. Note that "FTPS Implicit SSL" is supported by the Net library.
  4. AscySoft

    AscySoft Active Member Licensed User

    Well, I kinda post something on network library thread asking about if FTPS is implemented into Net library(because I know is based on apache.common.net witch implement this), but that thread is so long.... http://www.basic4ppc.com/forum/addi...rary-android-ftp-smtp-pop3-12.html#post153913

    EDIT: my fault, it seems that you posted an answer there as well. "me, silly" ;)
  5. AscySoft

    AscySoft Active Member Licensed User

    Is there any way to determine if current sftp connection is initialized?
  6. Erel

    Erel Administrator Staff Member Licensed User

    The connection is established automatically when needed.
  7. AscySoft

    AscySoft Active Member Licensed User

    I see.. on my tests indeed connection was almost instantly (re)enabled.
    Is not exactly like in net.ftp, but even better.
    This lib is excellent
    Thanks again.:sign0098:
  8. lorebarita

    lorebarita Member Licensed User

    Synchro feature and searching for files in SFTP

    Hi, the library is very nice and would like to know if it is possible to add these features:
    - synchro feature where one can set a file or folder and automatically it syncrhonizes android2server or server2android
    - possibility of searching for a file if its not known where it is located within subfolders when trying to download (the root where the file is, is known but not subfolders name)
    - possibility to know the datetime of the file on the server

  9. Erel

    Erel Administrator Staff Member Licensed User

    You can get the time stamp with the List command.

    The other two features can be implemented in your code. It will not be easier to implement them in the library code.
  10. lorebarita

    lorebarita Member Licensed User

    Thanks, we will try to do it!
  11. boredsilly

    boredsilly Member Licensed User

    Command execution possible?

    Is it possible to execute a remote command as it is with standard ftp as I'd like to chmod the files on the server once uploaded.
  12. Erel

    Erel Administrator Staff Member Licensed User

    Currently it is not possible. Which commands do you need to execute?
  13. boredsilly

    boredsilly Member Licensed User

    I'd like to be able to rename the file extension from .tmp to .new when upload is complete (a means for the server to know the file is completely uploaded) also I may have to chmod (change permissions) on the file.
  14. AscySoft

    AscySoft Active Member Licensed User

  15. Erel

    Erel Administrator Staff Member Licensed User

    V1.10 is released. It is available in the first post. It includes support for Chmod and Rename commands.
  16. boredsilly

    boredsilly Member Licensed User

    Thanks , that's greatly appreciated.
  17. AscySoft

    AscySoft Active Member Licensed User

    Thank you.
    Nice job!
  18. Rusty

    Rusty Well-Known Member Licensed User

    Any chance there is an example project to view?
  19. Erel

    Erel Administrator Staff Member Licensed User

    See the first post for the required code.
  20. Kwame Twum

    Kwame Twum Active Member Licensed User

    Not working for me

    When I try to upload a file to the server or download a file from it, I get a Timed Out error after a while.

    Sub Activity_Create(FirstTime As Boolean)
        bu.Text = 
    "ttp""username""password","xxxxxxxxxx.com"22'Credentials are however correct - tested with desktop ftp client
    End Sub
    Sub buu_Click
    Dim dial As FileDialog
    File.Combine(File.DirRootExternal, "/")
    If dial.Show("Select a file""Lego!""Cancel","",Null)=-1 Then
    Log("Attempting to upload " & dial.FilePath &  "/" & dial.ChosenName)
            tfp.UploadFile(dial.FilePath & 
    "/" , dial.ChosenName, "palztech/palztechgroup.com/wwwroot/" & dial.ChosenName)
    End If
    End Sub
    Logs after running and selecting a file to upload
    Attempting to upload /mnt/sdcard/bluetooth/00001.vcf
    00001.vcf, Success=false
    java.net.SocketTimeoutException: Connection timed out
    Am I doing somthing wrong?
  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