1

I've seen a number of examples on how to do this with only one file, but I'd like to accept two lists of files over a single REST call and reference each list by name. I'm using this as my method:

@POST
@Path("/initialize")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String initialize(MultiPart data) {
    List<File> maps = new ArrayList<>();
    List<File> contexts = new ArrayList<>();

    for(BodyPart bodyPart : data.getBodyParts()){
        String name = bodyPart.getContentDisposition().getParameters().get("name");
        if(name != null && name.equals("maps")){
            //get the files from this bodyPart and add them to maps
        } else if (name != null && name.equals("contexts")){
            //get the files from this bodyPart and add them to contexts
        }
    }
    return "foo";
}

Can anyone help me make the final step here and get the actual input streams out of the bodyParts? They have a getEntity() field, but I'm not sure that's what I'm looking for.

Alex Pritchard
  • 4,260
  • 5
  • 33
  • 48

1 Answers1

1

Try this:

@POST
@Path("upload")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.MULTIPART_FORM_DATA)
 public String uploadMultipleFile(
        FormDataMultiPart multiform) {

    BodyPartEntity bodyPartEntity;
    String uploadedFileLocation;
    for(BodyPart part: multiform.getBodyParts()){
        bodyPartEntity = (BodyPartEntity) part.getEntity();
        uploadedFileLocation = "c://test/" + part.getContentDisposition().getFileName();
        saveToFile(bodyPartEntity.getInputStream(), uploadedFileLocation);
    }
    return "OK";
}