Switch from commons-fileupload to commons-fileupload-jakarta-servlet6

I need to change my fileupload-integration to the jakarta6-version. The old code is
resp.setHeader("Transfer-Encoding", "chunked");
byte timeout = parseTimeoutParameter(req);
int port = parsePortParameter(req);
long expLen = Long.valueOf(req.getHeader(HttpHeaders.CONTENT_LENGTH));
ServletFileUpload upload = new ServletFileUpload();
ServletOutputStream servletOut = resp.getOutputStream();
FileItemIterator itemIterator = upload.getItemIterator(req);
while (itemIterator.hasNext()) {
FileItemStream streamItem = itemIterator.next();
if (streamItem.getFieldName().equals("ova")) {
String nameWithoutQuotas = streamItem.getName();
nameWithoutQuotas = nameWithoutQuotas.substring(0, nameWithoutQuotas.lastIndexOf('.'));
File tf = File.createTempFile("ova-rm", ".ova");
FileOutputStream fos = new FileOutputStream(tf);
copy(streamItem.openStream(), fos, servletOut, 1024 * 8, expLen);
fos.close();
try {
app.getBean(OVAImporter.class).importOVA(tf, nameWithoutQuotas, timeout, port);
} catch (Exception e) {
throw new ServletException(e);
}
}
}


resp.setHeader("Transfer-Encoding", "chunked");
byte timeout = parseTimeoutParameter(req);
int port = parsePortParameter(req);
long expLen = Long.valueOf(req.getHeader(HttpHeaders.CONTENT_LENGTH));
ServletFileUpload upload = new ServletFileUpload();
ServletOutputStream servletOut = resp.getOutputStream();
FileItemIterator itemIterator = upload.getItemIterator(req);
while (itemIterator.hasNext()) {
FileItemStream streamItem = itemIterator.next();
if (streamItem.getFieldName().equals("ova")) {
String nameWithoutQuotas = streamItem.getName();
nameWithoutQuotas = nameWithoutQuotas.substring(0, nameWithoutQuotas.lastIndexOf('.'));
File tf = File.createTempFile("ova-rm", ".ova");
FileOutputStream fos = new FileOutputStream(tf);
copy(streamItem.openStream(), fos, servletOut, 1024 * 8, expLen);
fos.close();
try {
app.getBean(OVAImporter.class).importOVA(tf, nameWithoutQuotas, timeout, port);
} catch (Exception e) {
throw new ServletException(e);
}
}
}


. Using the new version I have this constructor: new JakartaServletFileUpload<FileItem<I>, FileItemFactory<I>>();. How to use the constructor properly?
17 Replies
JavaBot
JavaBot6mo ago
This post has been reserved for your question.
Hey @Peter Rader! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Peter Rader
Peter RaderOP6mo ago
Hm, can you be more specific?
dan1st
dan1st6mo ago
Well that page has examples for using that constructor I think the first code example is pretty much what you want
Peter Rader
Peter RaderOP6mo ago
I found the example for constructor JakartaServletDiskFileUpload but I need JakartaServletFileUpload (without disk in the name).
dan1st
dan1st6mo ago
Just use your own ItemFactory? And for that you need to decide what kind of FileItem you want
Peter Rader
Peter RaderOP6mo ago
You mean FileItemFactory? I think there must be an easier way. To create my own factory for simple upload is a overkill.
dan1st
dan1st6mo ago
If you want a FileItemFactory, use that but idk whether you want one or whether you want something custom if you want a FileItemFactory, you can probably also use JakartaServletDiskFileUpload?
Peter Rader
Peter RaderOP6mo ago
Why should I use JakartaServletDiskFileUpload instead of JakartaServletFileUpload?
dan1st
dan1st6mo ago
If you want to save it to disk, that already has some logic implemented?
Peter Rader
Peter RaderOP6mo ago
I dont like to save all. I only like to save the .ova file.
dan1st
dan1st6mo ago
Is the no-args constructor not applicable?
Peter Rader
Peter RaderOP6mo ago
and only <?> generics?
dan1st
dan1st6mo ago
I think if you want to use something that doesn't save it on disk, you either need the no-args constructor or a custom factory start with them and change them as necessary
dan1st
dan1st6mo ago
Baeldung
Spring and Apache FileUpload | Baeldung
A quick and practical guide to using Apache FileUpload with Spring.
dan1st
dan1st6mo ago
section 4. might be interesting
JavaBot
JavaBot6mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server