0

I am working on a Jersey API which has to take in the input of multiple files along with some details about them.

For eg. For every image there will be the name of the person who clicked it, the category to which it belongs,the location where the picture was taken, etc. This has to be done on a single request Object.

There is no definite number of files so I can not create different variables like (file1, photographer1, category1, location1), (file2, photographer2, category2, location2)...., etc.

I shall be accepting the files in a list through multipart.

Is there an efficient way to fetch the image's meta data such that we can determine to which file/image in the list does the meta-data correspond to?

meowth
  • 43
  • 1
  • 2
  • 10
  • Yes it is possible. This is what multipart is for: mixing files with other data. – Paul Samsotha Aug 07 '18 at 08:20
  • this might help https://stackoverflow.com/questions/27643558/java-rest-jersey-posting-multiple-types-of-data-file-and-json – Vaibhav_Sharma Nov 22 '18 at 11:39
  • Hi @Vaibhav_Sharma, the link you shared does not tell about multiple files. What if we have a list of files..how shall we know that the meta data corresponds to which file? – meowth Jan 29 '19 at 06:23

1 Answers1

0

In order to get the metadata of some file you can refer below link: Get the metadata of a file

After getting metadata you can write some function to pass that information like below:

public void postMultipleFiles(List<String> fileNames, List<Object> metaData){
    FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
    final List<FileDataBodyPart> l1 = new ArrayList<>();
    final List<FormDataMultiPart> l2 = new ArrayList<>();
    for (int i = 0; i < fileNames.size(); i++) {
        final FileDataBodyPart f1 = new FileDataBodyPart("file", new File("path to store" + fileNames.get(i)));
        l1.add(f1);
        final FormDataMultiPart m1 = (FormDataMultiPart) formDataMultiPart.field("metadata", metaData.get(i), MediaType.APPLICATION_JSON_TYPE).bodyPart(f1);
        l2.add(m1);
    }
    final Response response = prepareToSend(path).post(Entity.entity(l2.get(0), l2.get(0).getMediaType()));
} 

Note: prepareToSend is a method that is returning a builder of a simple java client that you are using for API requests (GET/POST/PATCH) etc.

Hope this helps. :)

Vaibhav_Sharma
  • 546
  • 1
  • 9
  • 22