Jersey Client upload multiple files associated with named control

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Jersey Client upload multiple files associated with named control

Whelan, Andy


I am having trouble trying to figure out how to get the Jersey Client software to do something. I have an application that has the capability of uploading multiple files. See the following HTML.

<form action="http://localhost:8080/app/rest/files/uploadMultipleFiles" method="post"



   Select a file to Upload to server:

   <input type="file" name="files" size="60" multiple=“multiple”/> 


 <input type="submit" value="Upload File" /> 




The SERVER CODE does the following:




    public Response uploadFiles(final FormDataMultiPart multiPart)


      //Get contents of control named “files”

        List<FormDataBodyPart> bodyParts = multiPart.getFields("files");

        /* Save multiple files */

        if (bodyParts != null)



            for (int i = 0; i < bodyParts.size(); i++)



                BodyPartEntity bodyPartEntity = (BodyPartEntity) bodyParts.get(i).getEntity();

                String fileName = bodyParts.get(i).getContentDisposition().getFileName();



                    String path = temporaryUploadDirectory + File.separator + fileName;

                    long size = saveFile(bodyPartEntity.getInputStream(), path);


It uses  the HTML input control name “files” to obtain the list List<FormDataBodyPart> of uploaded files. It can then access each file as a BodyPartEntity.

I’m having great difficulty figuring out how to get a Jersey client to upload the code so that the files are associated with a control names “files”. I want to write client code to upload to the server this way (the HTML fragment before the server code hits it fine).

I can throw together code to create FileDataBodyPart for each file to upload:

List<FileDataBodyPart> bodyParts = new ArrayList<FileDataBodyPart>();

//get files to upload       

File dir = new File(classLoader.getResource("uploadTestDirectory").getFile());

if (dir.exists())


  File[] files = dir.listFiles();

  int count = 0;

  for (File f : files)


     //Create a FileDataBodyPart for each file from uploadTestDirectory. Add to a list

     bodyParts.add(new FileDataBodyPart("file" + count, new File(f.getAbsolutePath()),





//FOLLOWING IS THE FormDataMultiPart to upload

WebTarget webTarget ="http://localhost:8080/app/rest").path("files").path("uploadMultipleFiles");




//add the file body parts

for (FileDataBodyPart bp : bodyParts)




//call the web service to upload

Response clientResponse =

                webTarget.request(MediaType.APPLICATION_JSON).post(Entity.entity(multiPart, multiPart.getMediaType()));


My question: How can I associate the “files” control so files( the FileDataBodyPart objects)  are included under a control named “files” (like my server code expects, like the HTML at the top would produce).

I can upload files with client code like the above but can’t figure out a way to associate them with a “files” control. Does this make sense?

Any help would be appreciated.