3

I have a web form which contains a file upload option and a host of other input parameters. I'm looking for some way to handle this with a Jersey request handler where the method parameters would be the file input, and "all other parameters".

This question explains that I can't get the other parameters into a custom model object, because the browser sends them as separate multipart objects. The next thing I tried was retrieving the other parameters in a MultivaluedMap:

@POST
@Produces("text/html; charset=\"UTF-8\"")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Page handlePost(@FormDataParam("icon") InputStream iconInputStream,
        @FormDataParam("icon") FormDataContentDisposition iconContentDispositionHeader,
        MultivaluedMap<String, String> formParams) {
    ...
}

Unfortunately this does not work either.

There are about 20 other parameters in the form (one of which is a multiselect-option), so I wouldn't want to handle them one-by-one as method parameters. Is there any way in which I could get all the other parameters in a single object from which they could be queried?

Community
  • 1
  • 1
Sampo
  • 4,308
  • 6
  • 35
  • 51
  • What parameters are you taking about? The query parameters? The other multipart fields? – Paul Samsotha Jul 13 '16 at 09:08
  • 1
    If the latter you can get all of them using `@FormDataParam` also. Just set the value to the name of the field. And just use a string parameter. If you want to get all into one object, you can use a `@BeanParam`. It should works for multipart data also. Or you can use `FormDataMultiPart` as a parameter, and extract everything manually – Paul Samsotha Jul 13 '16 at 09:11

1 Answers1

0

What Paul suggested is the right direction. I just finished one RESTful Service as this:

@POST
@Path("fileupload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response uploadFile(@FormDataParam("component") String system, @FormDataParam("purpose") String purpose,  @FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail) {

.... ....}

And it works perfect

Victor Guo
  • 21
  • 2
  • That's how I implemented it finally, but now the method declaration has 20 `@FormDataParam` definitions. The question was about getting all of these in a `MultivaluedMap` or similar. – Sampo Mar 26 '18 at 19:43