Android Question SFTP How to made it...

TheArkhangel

Member
Licensed User
Longtime User
greetings to all
I'm trying to use SFTP instead of FTP to send images to a server securely.

I can not find the way to do that I have reviewed even JSch library but whenever I try to install the program to see if it works tells me downgrade fails.

I would like to know if anyone would know how to connect to an SFTP server and send a file.

Here is the code I use for FTP I have done.

B4X:
FTP.Initialize("FTP","192.168.1.71",21,"user","pass")
    dia=DateTime.GetDayOfMonth (DateTime.Now)
    mes=DateTime.GetMonth(DateTime.Now)
    año=DateTime.GetYear(DateTime.now)
    hora=DateTime.GetHour(DateTime.now)
    minutos=DateTime.GetMinute(DateTime.now)
    segundos=DateTime.GetSecond(DateTime.now)
    fecha=dia & mes & año & "_" & hora & minutos & segundos & ".jpg"
    Dim filename As String = fecha
    Dim dir As String = File.DirRootExternal
    camEx.SavePictureToFile(Data, dir, filename)
    camEx.StartPreview 'restart preview
    'send a broadcast intent to the media scanner to force it to scan the saved file.
    Dim Phone As Phone
    Dim i As Intent
    i.Initialize("android.intent.action.MEDIA_SCANNER_SCAN_FILE", "file://" & File.Combine(dir, filename))
    Phone.SendBroadcastIntent(i)
    ToastMessageShow("Imagen Grabada." & CRLF  & "File size: " & File.Size(dir, filename), True)
    FTP.UploadFile(File.DirRootExternal, fecha, False, "/Pacientes/" & "P" & Paciente & "/" & fecha)

As it is evident intent is to prevent can be listening to port 21 and locate the user and password.


I would appreciate some small code example or orientation.

Thanks to all
 

Daniel-White

Active Member
Licensed User
Longtime User
Are you code able to upload files? or the problem is specifically with the intent. etc. I mean, before you implemented the intent you tested you can upload any files to SFTP daemon.
 
Upvote 0

TheArkhangel

Member
Licensed User
Longtime User
Erel, thanks for you responde.....now I debug the tutorial and this is the code and the error.

B4X:
#Region  Project Attributes
    #ApplicationLabel: sftp test
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified

#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim sftp1 As SFtp
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Dim ListView1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
Try

     If FirstTime Then
         'use real credentials!
        sftp1.Initialize("sftp1", "Domingo Gonzalez", "12345", "192.168.1.71", 22)
        sftp1.SetKnownHostsStore(File.DirInternal , "hosts.txt")
       
    End If
    Activity.LoadLayout("1")
   
    AddInfo("Try send first file")
    sftp1.UploadFile (File.DirRootExternal ,"1.jpg","1.jpg")
   
    sftp1.List ("/ftp/*.*")
   
Catch
    AddInfo (LastException)
End Try
End Sub

Sub Activity_Resume
    sftp1.Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub sftp1_UploadCompleted (ServerPath As String, Success As Boolean)
    Log(ServerPath & ", Succes=" & Success)
    If Success = False Then
        'upload incomplete?
        Log(LastException.Message)
        sftp1.CloseNow
        Msgbox(LastException.Message,"Error:")
    End If
       
End Sub
Sub sftp1_CommandCompleted (Command As String, Success As Boolean, Reply As String)
    AddInfo (Command & " success=" & Success & " " & Reply)
End Sub

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

Sub sftp1_ShowMessage (Message As String)
    AddInfo (Message)
End Sub
Sub sftp1_ListCompleted (ServerPath As String, Success As Boolean, Folders() As SFtpEntry, Files() As SFtpEntry)
    If Success Then
        Dim i As Long
        For i = 0 To Files.Length -1
            AddInfo (Files(i).Name)
        Next
        AddInfo ("List command completed")
    Else
        AddInfo ("List command error")
    End If
End Sub


Sub AddInfo (msj As String )
    ListView1.AddSingleLine(msj)
End Sub
Sub Button1_Click
    sftp1.CloseNow
    ExitApplication
End Sub

It starts correctly, the message that you can not connect appears and if we accept the key ...... and this is the code that shows when storing it on the device .....

Screenshot_2016-07-26-14-21-32.png Screenshot_2016-07-26-14-21-51.png Screenshot_2016-07-26-14-22-06.png

What am I doing wrong ?
Thank you
 
Upvote 0

TheArkhangel

Member
Licensed User
Longtime User
I used filezilla ftp program to connect and send files and remove the key and the user are correct.
In the log I get this information only.

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
/1.jpg, Succes=false
com.jcraft.jsch.JSchException: Auth fail

The only I use a mac ssh ftp to server, and on the second image.....this is the correct path to create a save the hosts.txt file ?
 
Last edited:
Upvote 0
Top