Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read uploaded file details in Jersey

Tags:

java

jersey

I have implemented REST service for upload multiple files into server.But the problem was that i can not get file details if i use FormDataMultiPart in the REST service.I can get the file content.like wise i tried to get file details like following and it gave me an error saying not supported.

    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public void upload(FormDataMultiPart formParams)
    {
        Map<String, List<FormDataBodyPart>> fieldsByName = formParams.getFields();

       //Assume i am sending only files with the request

        for (List<FormDataBodyPart> fields : fieldsByName.values())
        {
            for (FormDataBodyPart field : fields)
            {

                InputStream is = field.getEntityAs(InputStream.class);
                String fileName = field.getName();
                field.getMediaType();
               //Those work perfectly

    //This gave me an error
  FormDataContentDisposition f=field.getEntityAs(FormDataContentDisposition   .class);
    System.out.println(f.getFileName());

            }
        }
    }

Please let me know how can i get files details like type,name,size for each uploaded files if use FormDataMultiPart.

like image 897
gihan-maduranga Avatar asked Jan 27 '16 12:01

gihan-maduranga


1 Answers

I was able to access file details finally here is the code snippet.

@POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public void upload(FormDataMultiPart formParams)
    {
        Map<String, List<FormDataBodyPart>> fieldsByName = formParams.getFields();

       //Assume i am sending only files with the request

        for (List<FormDataBodyPart> fields : fieldsByName.values())
        {
            for (FormDataBodyPart field : fields)
            {

                InputStream is = field.getEntityAs(InputStream.class);
                String fileName = field.getName();
                field.getMediaType();


   //This working fine
  FormDataContentDisposition f=field.getFormDataContentDisposition();
  System.out.println(f.getFileName());

            }
        }
    }
like image 65
gihan-maduranga Avatar answered Sep 22 '22 16:09

gihan-maduranga