B4J Question [SOLVED] FTP upload "426 Failure" when using Explicit Encryption mode

Ralph Parkhurst

Member
Licensed User
Longtime User
I am trying to implement file upload to an FTP server using Explicit encryption mode.

However it only works correctly about half the time. I regularly get an exception error "Error uploading file" and although most of the file gets transferred, around the last 40 or 50 bytes does not get copied.

Here is a small code segment which reproduces the issue:

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight:720
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private ftp As FTP
    
    Public const FTPHostName As String    = "HostName"  ' <<< insert
    Public Const FTPUsername As String     = "Username"  ' <<< insert
    Public Const FTPPassword As String    = "Password"  ' <<< insert
    
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
End Sub


Sub Button_Click
    'upload test
    Dim ftp As FTP
    ftp.Initialize("ftp1", FTPHostName, 21, FTPUsername, FTPPassword)
    ftp.PassiveMode = True
    ftp.UseSSLExplicit=True
    Dim ssltrustmanager As CustomTrustManager   
    ssltrustmanager.initializeAcceptAll
    ftp.SetCustomSSLTrustManager(ssltrustmanager)
    ftp.UploadFile("C:\", "audio.mp3", False, "/ftp/upload/audio.mp3")
    ftp.CLOSE
End Sub

Sub ftp1_UploadCompleted (serverpath As String, success As Boolean)
    If success =True Then
        Log("Upload successful")
    else if LastException.Message.Contains("426") Then
        Log("426 Warning")
    Else
        Log("Upload failure")
    End If
End Sub

and here is the log output showing three successful uploads and two failures due to a "426 Failure reading network stream" error:

B4X:
Upload successful
java.lang.RuntimeException: Error uploading file.
426 Failure reading network stream.
    at anywheresoftware.b4a.net.FTPWrapper$2.run(FTPWrapper.java:241)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
426 Warning
Upload successful
java.lang.RuntimeException: Error uploading file.
426 Failure reading network stream.
    at anywheresoftware.b4a.net.FTPWrapper$2.run(FTPWrapper.java:241)
    at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
426 Warning
Upload successful

I wonder if anyone can kindly help me resolve this issue - any suggestions will be warmly received.

I am using B4J 9.5.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should use Wait For: [B4X] Net library (FTP, SMTP, POP) with Wait For

Better to close the ftp connection after the Wait For line.

If it sometimes works and sometime doesn't then you should implement a retrial algorithm. Something like:
B4X:
Do While True
 
 Dim ftp As FTP
    ftp.Initialize("ftp1", FTPHostName, 21, FTPUsername, FTPPassword)
    ftp.PassiveMode = True
    ftp.UseSSLExplicit=True
    Dim ssltrustmanager As CustomTrustManager   
    ssltrustmanager.initializeAcceptAll
    ftp.SetCustomSSLTrustManager(ssltrustmanager)
    ftp.UploadFile("C:\", "audio.mp3", False, "/ftp/upload/audio.mp3")
    Wait For ftp1_UploadCompleted (serverpath As String, success As Boolean)
     ...
    ftp.Close
    If Success Then Return
    Sleep(1000)
Loop
 
Upvote 0

Ralph Parkhurst

Member
Licensed User
Longtime User
Many thanks Erel. Great ideas - I will try now and advise.
Warm regards,
Ralph
This seems to have solved the problem. I am still getting the "426 Failure" errors, but a lot less frequently now. And when the 426 Failure does occur the retry fixes things nicely.

This should work well for me and I cannot thank you enough for your prompt assistance - I have been battling with this for a while.
 
Upvote 0
Top