B4J Question Multipart Data

Peter Lewis

Active Member
Licensed User
Longtime User
Hi All, I have been trying to extract the data from this list that outputs on the log as

Tried to extract it using Arrays, Maps, List and Multipart data. None of those are working for me.

This is the basic code - Cannot find the Code insert

B4X:
[QUOTE]
Dim folder As String ="r:\temp\"
    Dim maxsize As Long = 999999999999

    Dim piclist As List = GetMultiparts(req ,folder,maxsize)

Log(piclist.Size)

For j=0 To piclist.size-1
    Log(piclist.Get(j))

Next
[/QUOTE]


25328894
17
Part{n=,fn=null,ct=null,s=0,t=true,f=null}
Part{n=,fn=null,ct=null,s=0,t=true,f=null}
Part{n=,fn=null,ct=null,s=0,t=true,f=null}
Part{n=,fn=null,ct=null,s=0,t=true,f=null}
Part{n=,fn=null,ct=null,s=0,t=true,f=null}
Part{n=,fn=null,ct=null,s=0,t=true,f=null}
Part{n=,fn=null,ct=null,s=0,t=true,f=null}
Part{n=,fn=null,ct=null,s=0,t=true,f=null}
Part{n=,fn=null,ct=null,s=0,t=true,f=null}
Part{n=gallerypics,fn=null,ct=null,s=0,t=true,f=null}
Part{n=gallerypics,fn=pic4.jpg,ct=image/jpeg,s=4311388,t=true,f=r:\temp\MultiPart10749281659612284644}
Part{n=gallerypics,fn=pic5.jpg,ct=image/jpeg,s=4049843,t=true,f=r:\temp\MultiPart1733147283869530057}
Part{n=gallerypics,fn=pic8.jpg,ct=image/jpeg,s=3763956,t=true,f=r:\temp\MultiPart14608056935354210937}
Part{n=gallerypics,fn=pic9.jpg,ct=image/jpeg,s=4305587,t=true,f=r:\temp\MultiPart7925761567132071697}
Part{n=gallerypics,fn=pic10.jpg,ct=image/jpeg,s=3658261,t=true,f=r:\temp\MultiPart16777310328100045637}
Part{n=gallerypics,fn=5d1071f1-ae3d-473e-ab0e-009c4efdbc0d.jpg,ct=image/jpeg,s=582726,t=true,f=r:\temp\MultiPart6510141267914141141}
Part{n=gallerypics,fn=pic1.jpg,ct=image/jpeg,s=4655160,t=true,f=r:\temp\MultiPart12670979917436383701}
 

Attachments

  • 1631272431814.png
    1631272431814.png
    5.6 KB · Views: 189
Last edited:

Daestrum

Expert
Licensed User
Longtime User
1, For code just type [ code ] and [ /code ] without the spaces, around the code you want to show.

2, try (it looks like Part is defined as a type/class somewhere)
B4X:
For j=0 To piclist.size-1
Dim current as JavaObject = piclist.Get(j)
Log(current.GetField(n))
Log(current.GetField(fn))
Log(current.GetField(ct))
Log(current.GetField(s))
Log(current.GetField(t))
Log(current.GetField(f))
Next

if you know where Part is defined you could use
B4X:
For Each p As Part In piclist
log(p.n)
log(p.fn)
...
Next

**Not tested
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
1, For code just type [ code ] and [ /code ] without the spaces, around the code you want to show.

2, try (it looks like Part is defined as a type/class somewhere)
B4X:
For j=0 To piclist.size-1
Dim current as JavaObject = piclist.Get(j)
Log(current.GetField(n))
Log(current.GetField(fn))
Log(current.GetField(ct))
Log(current.GetField(s))
Log(current.GetField(t))
Log(current.GetField(f))
Next

if you know where Part is defined you could use
B4X:
For Each p As Part In piclist
log(p.n)
log(p.fn)
...
Next

**Not tested
Thank you for that,
The variable names did not work so I Dimmed them as strings , did not work so I put them in inverted commas - did not work.
When I put as a string - this was the error code
25328894
17
Part{n=,fn=null,ct=null,s=0,t=true,f=null}
Error occurred on line: 27
java.lang.RuntimeException: Field: not found in: org.eclipse.jetty.util.MultiPartInputStreamParser$MultiPart
at anywheresoftware.b4j.object.JavaObject$FieldCache.getField(JavaObject.java:307)
at anywheresoftware.b4j.object.JavaObject.GetField(JavaObject.java:182)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at jdk.internal.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:95)
at anywheresoftware.b4j.object.JServlet$Handle.run(JServlet.java:130)
at anywheresoftware.b4j.object.JServlet.Handle(JServlet.java:110)
at anywheresoftware.b4j.object.JServlet.doPost(JServlet.java:80)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:865)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1655)
at anywheresoftware.b4j.object.JServlet$Handle.run(JServlet.java:135)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:47)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:43)
at anywheresoftware.b4a.shell.ShellBA.startMessageLoop(ShellBA.java:119)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:170)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:309)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:95)
at b4j.example.main.main(main.java:29)
-1
0

When I put as Log(current.GetField("n")) then i go this error code

ID received /portal/vendorlistingsadd/listing/f8bca21a-8198-4e76-908e-a6591294bd71
25328894
17
Part{n=,fn=null,ct=null,s=0,t=true,f=null}
Error occurred on line: 27
java.lang.RuntimeException: Field: n not found in: org.eclipse.jetty.util.MultiPartInputStreamParser$MultiPart
at anywheresoftware.b4j.object.JavaObject$FieldCache.getField(JavaObject.java:307)
at anywheresoftware.b4j.object.JavaObject.GetField(JavaObject.java:182)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at jdk.internal.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:95)
at anywheresoftware.b4j.object.JServlet$Handle.run(JServlet.java:130)
at anywheresoftware.b4j.object.JServlet.Handle(JServlet.java:110)
at anywheresoftware.b4j.object.JServlet.doPost(JServlet.java:80)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:865)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1655)
at anywheresoftware.b4j.object.JServlet$Handle.run(JServlet.java:135)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:47)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:43)
at anywheresoftware.b4a.shell.ShellBA.startMessageLoop(ShellBA.java:119)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:170)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:309)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:95)
at b4j.example.main.main(main.java:29)
-1
0

The initial code that defines the part was done by Erel

B4X:
Public Sub GetMultiparts(req As ServletRequest, folder As String, maxsize As Long) As List
  Dim config As JavaObject
  config.InitializeNewInstance("javax.servlet.MultipartConfigElement", Array(folder, maxsize, maxsize, 81920))
  Dim f As  JavaObject
  f.InitializeNewInstance("java.io.File", Array(folder))
  Dim parser As JavaObject
  parser.InitializeNewInstance("org.eclipse.jetty.util.MultiPartInputStreamParser", Array(req.InputStream, req.ContentType, config, f))
  Dim parts As JavaObject = parser.RunMethod("getParts", Null)
  Dim result() As Object = parts.RunMethod("toArray", Null)
  Return result
End Sub
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
B4X:
For Each p() As Object In piclist
Log(p(0))   ' first field 'n'
Log(p(1))  ' second field 'fn'
...
Log(p(5))   ' last field 'f'
next
 
Last edited:
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
Thank you,

I have tried so many options and this one also gives an error

B4X:
    Dim folder As String ="r:\temp\"
    Dim maxsize As Long = 999999999999
    Dim piclist As List = GetMultiparts(req ,folder,maxsize)
    Log(piclist.Size)
For j=0 To piclist.size-1
        Log(piclist.Get(j))
        For Each q() As Object In piclist
            Log(q(0))   ' first field 'n'
            Log(q(1))  ' second field 'fn'
            Log(q(5))   ' last field 'f'
        Next
Next

there are so many rows coming in at null, would that not be causing a problem. ? I looked at the html and all looks good.



ID received /portal/vendorlistingsadd/listing/f8bca21a-8198-4e76-908e-a6591294bd71
15947734
11
Part{n=,fn=null,ct=null,s=0,t=true,f=null}
Error occurred on line: 26 (clsUploadGallery)
java.lang.ClassCastException: class org.eclipse.jetty.util.MultiPartInputStreamParser$MultiPart cannot be cast to class [Ljava.lang.Object; (org.eclipse.jetty.util.MultiPartInputStreamParser$MultiPart is in unnamed module of loader 'app'; [Ljava.lang.Object; is in module java.base of loader 'bootstrap')
at b4j.example.clsuploadgallery._handle(clsuploadgallery.java:146)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:632)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at jdk.internal.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:95)
at anywheresoftware.b4j.object.JServlet$Handle.run(JServlet.java:130)
at anywheresoftware.b4j.object.JServlet.Handle(JServlet.java:110)
at anywheresoftware.b4j.object.JServlet.doPost(JServlet.java:80)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:865)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1655)
at anywheresoftware.b4j.object.JServlet$Handle.run(JServlet.java:135)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:47)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:43)
at anywheresoftware.b4a.shell.ShellBA.startMessageLoop(ShellBA.java:119)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:170)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:309)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:95)
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
It looks like the first few item are invalid
Part{n=,fn=null,ct=null,s=0,t=true,f=null}

Also sorry I didn't make it clearer
this
B4X:
For j=0 To piclist.size-1
        Log(piclist.Get(j))
        For Each q() As Object In piclist
            Log(q(0))   ' first field 'n'
            Log(q(1))  ' second field 'fn'
            Log(q(5))   ' last field 'f'
        Next
Next

Should be
B4X:
        For Each q() As Object In piclist
            Log(q(0))   ' first field 'n'
            Log(q(1))  ' second field 'fn'
            Log(q(5))   ' last field 'f'
        Next

You can probably skip items with an S=0 ie
B4X:
        For Each q() As Object In piclist
            if q(3) > 0 then   ' s field
            Log(q(0))   ' first field 'n'
            Log(q(1))  ' second field 'fn'
            Log(q(5))   ' last field 'f'
            End if
        Next
 
Upvote 0

Peter Lewis

Active Member
Licensed User
Longtime User
Thank you @Daestrum for your help,

I tested your solution and still had an error

multipart/form-data; boundary=----WebKitFormBoundaryk5HYVcBtXBEdXkAY
252400
13
Error occurred on line: 38
java.lang.IllegalArgumentException: Argument is not an array
at java.base/java.lang.reflect.Array.get(Native Method)
at anywheresoftware.b4a.shell.ArraysUtils.getElement(ArraysUtils.java:76)
at anywheresoftware.b4a.shell.Shell.getArrayElement(Shell.java:482)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:269)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at jdk.internal.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:95)
at anywheresoftware.b4j.object.JServlet$Handle.run(JServlet.java:130)
at anywheresoftware.b4j.object.JServlet.Handle(JServlet.java:110)
at anywheresoftware.b4j.object.JServlet.doPost(JServlet.java:80)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:865)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1655)
at anywheresoftware.b4j.object.JServlet$Handle.run(JServlet.java:135)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:47)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:43)
at anywheresoftware.b4a.shell.ShellBA.startMessageLoop(ShellBA.java:119)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:170)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:309)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:108)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:95)
at b4j.example.main.main(main.java:29)
-1
0

So spoke to @EnriqueGonzalez
and we came up with a solution

B4X:
For Each q As Part In piclist
   
    If File.Size("",q.TempFile) > 0 Then
            Log(q.SubmittedFilename)
            Log(q.TempFile)
    End If
Next

The output is now perfect

multipart/form-data; boundary=----WebKitFormBoundary2QZ49DclFrAAFKcQ
252400
13
704337d3-526a-4f23-ad1a-e08a86010007.jpg
R:\temp\MultiPart11055134646866722083
f8bca21a-8198-4e76-908e-a6591294bd71.jpg
R:\temp\MultiPart1692671330704033114
3cdb1056-f0f1-4008-92c3-74079d9248bb.jpg
R:\temp\MultiPart10130409954491560717
56ac854f-b854-4bbf-a37c-f487cac28b9b.jpg
R:\temp\MultiPart9593845080616775779
55887bd3-ea6c-43bf-ab2a-245f2cfb88fb.jpg
R:\temp\MultiPart7977197111130885901
-1
0
 
Upvote 0
Top