Hi,
I was trying to upload some image files from my b4a app to my B4J server.
Some files were uploaded successfully but some file upload with incorrect file size.
I noticed that when the first byte value is 0xFF , it will be omited and the file on the server will be one byte smaller than the source file.
My b4a app code is : (it used OKhttputils2)
and
server code is :
Result Log is
it is important that ant test be performed using diffrent image files including my attached image file
it seems that there is bug in okHttp library when OkHttpRequest object is initialized with InitializePost2 method
I was trying to upload some image files from my b4a app to my B4J server.
Some files were uploaded successfully but some file upload with incorrect file size.
I noticed that when the first byte value is 0xFF , it will be omited and the file on the server will be one byte smaller than the source file.
My b4a app code is : (it used OKhttputils2)
B4X:
Sub UploadFile
Log("File size is : " & File.Size(File.DirDefaultExternal, "myFile.jpg"))
Dim In As InputStream = File.OpenInput(File.DirDefaultExternal, "myFile.jpg")
Dim data(In.BytesAvailable ) As Byte
In.ReadBytes(data, 0, data.Length )
Log( "data length = " & data.Length)
Dim j As HttpJob
j.Initialize("file", Me)
j.PostBytes(link & "?type=file&filename=" & "myFile.jpg", data)
End Sub
and
server code is :
B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
If req.Method <> "POST" Then
resp.SendError(500, "method not supported.")
Return
End If
'we need to call req.InputStream before calling GetParameter.
'Otherwise the stream will be read internally (as the parameter might be in the post body).
Dim In As InputStream = req.InputStream
Dim reqType As String = req.GetParameter("type")
Select reqType
Case "text"
Dim tr As TextReader
tr.Initialize(In)
resp.Write("Message received successfully.")
Case "file"
Dim su As StringUtils
Dim filename As String =su.DecodeUrl(req.GetParameter("filename"),"UTF8")
If filename = "" Then
resp.SendError(500, "Missing filename parameter")
Return
End If
Dim out As OutputStream = File.OpenOutput(Main.filesFolder, filename, False)
File.Copy2(In, out)
out.Close
resp.Write("OK, file uploaded successfully : " & filename & ", size=" & File.Size(Main.filesFolder, filename))
End Select
End Sub
Result Log is
B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
File size is : 35300
data length = 35300
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
Success: OK, file uploaded successfully : myFile.jpg, size=35299
it seems that there is bug in okHttp library when OkHttpRequest object is initialized with InitializePost2 method