ServletRequest.GetMultipartData: target folder is no longer created automatically
This is not strictly a JavaObject API/package change, but it is a behavioral difference I encountered while migrating from jServer 4 / Jetty 11 to jServer 5.02 / Jetty 12.1.10, so it might be useful to document it here as well.
Consider code that uses a separate upload folder for each HTTP session:
B4X:
Dim UploadFolder As String = File.Combine(Main.UploadFolder, Session.Id)
Dim Parts As Map = req.GetMultipartData(UploadFolder, 10000000)
Assume that
Main.UploadFolder exists, but the session-specific subfolder does not yet exist.
jServer 4 / Jetty 11
This worked without explicitly creating the session-specific folder. The Jetty 11 multipart implementation created the configured temporary directory when it did not exist:
Java:
if (!Files.exists(_tmpDir))
Files.createDirectories(_tmpDir);
Source:
Jetty 11 MultiPartFormInputStream
jServer 5.02 / Jetty 12.1.10
With jServer 5, the same call fails if the supplied folder does not already exist:
Code:
java.nio.file.NoSuchFileException:
...\upload\<session-id>\MultiPart...
at java.base/java.nio.file.Files.createTempFile(...)
at anywheresoftware.b4j.object.JServlet$ServletRequestWrapper.GetMultipartData(JServlet.java:309)
The jServer 5 implementation uses the new Jetty multipart parser. When an uploaded file part is still held in memory,
GetMultipartData converts it to a temporary file with:
Java:
Path f = Files.createTempFile(Path.of(Folder), "MultiPart", "");
At this point the supplied folder is not created first.
Source:
jServer JServlet.GetMultipartData
Workaround for jServer 5
Explicitly create the folder before calling
GetMultipartData:
B4X:
Dim UploadFolder As String = File.Combine(Main.UploadFolder, Session.Id)
If File.IsDirectory(UploadFolder, "") = False Then
File.MakeDir(UploadFolder, "")
End If
Dim Parts As Map = req.GetMultipartData(UploadFolder, 10000000)
This preserves session-specific upload directories and works independently of whether the multipart implementation creates the destination folder internally.
The behavior appears to come from the new implementation of
GetMultipartData in jServer 5 rather than being a general Jetty 12 restriction.
Would it make sense for
GetMultipartData to create the supplied folder, restoring the behavior seen with jServer 4?