jojenki
jojenki

Reputation: 255

Spring 3 MVC and Multiparts

I am using Spring 3's MVC framework, primarily just for the annotations.

As an example, the user will upload something like:

------------------------------6526f0c735cd
Content-Disposition: form-data; name="data"

[
  {
    "someField":"foo",
    "someNestedObject":{
      "someDeeperField":"bar",
      "someResourceField":"25f33030-12ed-4636-b5f7-759d27c40f08"
      ...
    }
  },
  ...
]

------------------------------6526f0c735cd
Content-Disposition: form-data; name="25f33030-12ed-4636-b5f7-759d27c40f08"; filename="some.jpg"
Content-Type: image/jpeg

<Binary Data>

Note that the name of the second part is part of the data in the first part. Then my code looks like:

@RequestMapping("test")
public static @ResponseBody void test(
    @RequestParam("data") final String data,
    final HttpServletRequest request)
    throws Exception {

    System.out.println(request.getParts());
}

It prints out an empty list because Spring has already consumed the Parts InputStream for its own processing. This is because I am using a MultipartResolver, which is something I need.

My problem is that, I still need access to the parts. I cannot use the @RequestPart annotation because I won't know the names of the parts until after I have processed the data part. I need to be able to ask Spring for one of the parts programmatically as opposed to as an argument to the function.

Thanks in advance.

Upvotes: 1

Views: 752

Answers (1)

jojenki
jojenki

Reputation: 255

I found a solution. I am using @RequestPart("media") final List<MultipartFile> media. All parts that are binary (media) data have the name "media", and their filename is their unique identifier.

I still like @RequestPart final Map<String, MultipartFile> media, but I will concede that it is really just syntactic sugar at this point.

Upvotes: 2

Related Questions