Parsing Multipart/mixed in Spring

Viewed 915

I want to parse requests like the following in my RestController

POST http://#.#.#.#:#/report HTTP/1.1
User-Agent: Android
Accept: text/html,application/xml,application/json,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Content-Type: multipart/mixed; boundary=%&REPORT_DIVIDER&%
Authorization: Basic ***
Content-Length: 23236
Host: #.#.#.#:#
Connection: Keep-Alive
Accept-Encoding: gzip


--%&REPORT_DIVIDER&%
Content-Type: application/json

{"content-excluded":true}
--%&REPORT_DIVIDER&%
Content-Disposition: attachment; filename="INSTALLATION"

Content-Type: application/octet-stream
609903cf-fcc0-460c-87db-958e031ac156
--%&REPORT_DIVIDER&%--

This message is conform to rfc1341, yet I cannot find a way to parse this in Spring.

Note that the number of files may vary.

I've already tried using CommonsMultipartResolver or StandardServletMultipartResolver, but both only support multipart/form-data (rfc1867).

Is there any way to parse these request in Spring besides writing my own parser?

2 Answers

While it is not a very nice solution, it was the only one I found:

I essentially reimplemented ServletFileUpload with this class and then loaded it into the apache library with this simple subclass of CommonsMultipartResolver

    @Path("api")
    @PUT
    @Consumes("multipart/mixed")
    @Produces("multipart/mixed")
    public MultiPart twelve( MultiPart multiPart) throws IOException {

    List<BodyPart> bodyParts = multiPart.getBodyParts();
    BodyPartEntity bpe = (BodyPartEntity) bodyParts.get(1).getEntity();
}

This is for jersey based spring boot project.

Related