Uploading files in Magnolia CMS

We need to have an upload functionality in our web application based on Magnolia CMS. So I’ve created a small servlet which uses commons-upload library from Apache. So the code looked like:

FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(request);

However the list of items was empty. Trying to understand where does my file goes – I’ve started debugging the library and it appears that MalformedStreamException(“Stream ended unexpectedly”) is thrown internally. However it’s silently skipped and you get no idea about what’s happening.

It appears that some of the magnolia filters preprocess the content and changes it. The right way to read content from the request in case of Magnolia would be:

MultipartForm form = (MultipartForm) req.getAttribute(MultipartForm.REQUEST_ATTRIBUTE_NAME);
Document document = form.getDocument("imageFile");

So the conclusion is: don’t use commons-upload with Magnolia CMS.