'Class module
' Adaptation from Erel's (Anywhere Software) post:
' https://www.b4x.com/android/forum/threads/server-upload-files-from-your-b4a-app-to-your-b4j-server-over-the-internet.37201/#content
Sub Class_Globals
End Sub
Public Sub Initialize
End Sub
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("name")
If reqType = "" Then
resp.SendError(500, "Missing name parameter.")
Return
End If
Dim name As String = req.GetParameter("name")
StoreFile(In, name)
' name = File.GetName(name) ' Strip it down to just the filename in case of "directory injection"
' Dim out As OutputStream = File.OpenOutput(Main.filesFolder, name, False)
' File.Copy2(In, out)
' out.Close
Log("Received file: " & name & ", size=" & File.Size(Main.filesFolder, name))
resp.Write("File received successfully.")
End Sub
Sub StoreFile(In As InputStream, name As String)
name = File.GetName(name) ' Strip it down to just the filename in case of "directory injection"
Dim out As OutputStream = File.OpenOutput(Main.filesFolder, name, False)
File.Copy2(In, out)
out.Close
End Sub