简体   繁体   中英

How to receive 2 binary files and JSON in Jersey jax-rs?

I need to build a service that can receive 2 binary files (~100k each) and some metadata, preferably in json.

I found this , but it only seems to provide one InputStream to one of the parts. But I'd need two.. so what to do?

You have a few options

  1. Simply add another parameter(s) with a different part annotation

     @POST @Consumes("multipart/form-data") public Response post(@FormDataParam("file1") InputStream file1, @FormDaraParam("file2") InputStream file2) { } 
  2. The parts can have the same part name, so you could do

     @POST @Consumes("multipart/form-data") public Response post(@FormDataParam("file") List<FormDataBodyPart> files) { for (FormDataBodyPart file: files) { FormDataContentDisposition fdcd = file.getFormDataContentDisposition(); String fileName = fdcd = getFileName(); InputStream is = file.getValueAs(InputStream.class); } } 
  3. You could traverse the entire multipart body youself

     @POST @Consumes("multipart/form-data") public Response post(FormDataMultiPart mulitPart) { Map<String, List<FormDataBodyPart>> fields = multiPart.getFields(); } 

See Also:

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