Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload multiple json files and form data to rest service

I would like to upload multiple JSON files (like student grades JSON, and student courses schedule JSON, student assignment JSON etc) and metadata (like student information)
To Rest service running on Jersy and tomcat

What is the approach to take here? should it be like a single controller? is it possible to specify the uploaded JSOn structure? what if one of the files is missing?

@Path("/submitStudentInformation")
public class SubmitStudInfoController {
    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces("text/plain")
    @Path("/multipleFiles")
public Response uploadFiles(@Context HttpServletRequest request) {
like image 454
JavaSheriff Avatar asked Sep 17 '17 18:09

JavaSheriff


1 Answers

Send the files list in rest api

@POST
@Path("/uploadFile") 
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response uploadFile(@FormDataParam("files") List<FormDataBodyPart> files)
 if(files!=null) {
     for (int i = 0; i < files.size(); i++) {
          FormDataBodyPart this_formDataBodyPartFile = files.get(i);
          ContentDisposition this_contentDispositionHeader = this_formDataBodyPartFile.getContentDisposition();
          InputStream this_fileInputStream = this_formDataBodyPartFile.getValueAs(InputStream.class);
          FormDataContentDisposition fileDetail = (FormDataContentDisposition) this_contentDispositionHeader;
          String imagename = fileDetail.getFileName();
     }
} 

Front end i am using angularjs so i set multiple files in formdata

var formdata = new FormData();
$scope.getTheFiles = function(element) {
    $scope.$apply(function($scope) {
        $scope.files = element.files;
        for (var i = 0; i < element.files.length; i++) {
            formdata.append('files', element.files[i]);
        }
    });
};
like image 64
Raja Ramachandran Avatar answered Nov 07 '22 17:11

Raja Ramachandran