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:
and here is the log output showing three successful uploads and two failures due to a "426 Failure reading network stream" error:
I wonder if anyone can kindly help me resolve this issue - any suggestions will be warmly received.
I am using B4J 9.5.
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.