Processing multipart upload stream in Java

691 Views Asked by At

I have this method to "process multipart upload" stream:

public class FileUpload {
    public static FileItemIterator parse(final byte[] data, final String contentType) {
        try {
            final ServletFileUpload upload = new ServletFileUpload();
            final HttpServletRequest request = new MockHttpServletRequest(data, contentType);
            final boolean isMultipart = ServletFileUpload.isMultipartContent(request);
            if ((!isMultipart)) {
                throw new Exception("Illegal request for uploading files. Multipart request expected.");
            }
            FileItemIterator iter = upload.getItemIterator(request);
            return iter;
        } catch (Throwable _e) {
            throw Exceptions.sneakyThrow(_e);
        }
    }
}

Where data is a InputStream converted into a byte array:

-----------------------------8721656041911415653955004498
Content-Disposition: form-data; name="myFile"; filename="test.txt"
Content-Type: text/plain

Simple file.
-----------------------------8721656041911415653955004498--

With contentType being this multipart/form-data; boundary=-----------------------------8721656041911415653955004498

The problem with this code is that the FileIterator is always empty. I think the data parameter here is correct and is based on MIME standard. What could be the reason that the iterator would be empty?

ps. This is a stand alone Uber Jar-type of program and thus not Servlet-based

0

There are 0 best solutions below