B4J Question JRDC2 Question

NikB4x

Member
Licensed User
Longtime User
Hi All,
I'm trying to add the file download handler to JRDC2:
with this code on linux server:

B4X:
'Handler class
Sub Class_Globals
    
End Sub

Public Sub Initialize
    
End Sub

Sub Handle(req As ServletRequest, resp As ServletResponse)
    Dim out As OutputStream
    Dim name As String = req.GetParameter("name")
    Log(name)
    Dim In As InputStream = File.OpenInput("/opt/jrdc2/files", name)
    out.InitializeToBytesArray(0)
    File.Copy2(In, out)
    In.Close
    Log("File " & File.Combine("/opt/jrdc2/files", name) & " Send!")
End Sub

In the B4A side I have this code:

B4X:
Sub ricevifile(filename As String)
    Dim j As HttpJob
    j.Initialize("", Me)
    Dim out As OutputStream
    j.Download2(ModVariabili.Server_Download,Array As String ("name",filename))
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim out As OutputStream = File.OpenOutput(File.Combine(File.DirInternal,"audio"), filename, False)
        out.InitializeToBytesArray(0)
        File.Copy2(j.GetInputStream, out)
        out.Close
    End If
    j.Release
End Sub

I don't receive any error, but the file in android side at the end of the procedure, it's empty (0 bytes).
Anyone could tell me where I'm doing wrong?
Thanks in advance!
 

DonManfred

Expert
Licensed User
Longtime User
it's empty (0 bytes).
I don´t see you using the resp obj to return anything to the caller!?

slc_externaltools_092.png


B4X:
 Dim out As OutputStream = resp.Outputstream
NOW you can write to the outputstream

You probably need to remove the outputstream initialization.
B4X:
'out.InitializeToBytesArray(0)
 
Last edited:
Upvote 0

NikB4x

Member
Licensed User
Longtime User
Thank you both very much for the fast replies!!

@DonManfred : You are right, I missed the result to send, now it works!

@Erel : I prefer to not publish the file to download cause it depends on the replies based on the query sent to the JRDC2, so it is created "on-fly" and deleted after it was sent. I don't know if there is a better way to do it and if I'm doing the better manner, in any case I want to thank you a lot not only for the very useful B4X developer environment, but also for all the tools and improvements that you are doing daily.
 
Upvote 0
Top