简体   繁体   中英

multipart/form-data file upload + other parameters in Jersey

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM