Jersey FormData, upload multiple file data

user3211037

I have written a Rest Service in Jersey to upload a Multiple Files. As shown Below. But i want the attribute name i.e name="metadata" and name="file " in Restservice class.

        Select XML file 1: <input type="file" **name="metadata"** size="45" accept=".xml" />


        Select PDF file 2: <input type="file" **name="fileak**" size="45"  accept=".pdf" />

Select XML file 1:

Select PDF file 2:

@POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)
    public Response uploadFile(
            @FormDataParam("file") List<FormDataBodyPart> parts) {
        for (FormDataBodyPart part : parts) {
            FormDataContentDisposition disp = part
                    .getFormDataContentDisposition();
            InputStream in = part.getValueAs(InputStream.class);
        }

        return Response.ok(" uploaded successfully !!").build();
    }

FormDataContentDisposition only pulls the content type , file name from the form and not the input type name="" attribute.

Any help would be highly appreciated.

I am posting an request using HTML as show below.

Posting the HTML file as wel.

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
        <meta content="utf-8" http-equiv="encoding" />

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script type="text/javascript">

        $(document).ready(function()
        {
            $("#uploadBtn").click(function()
            {
                $('input[type="file"]').each(function(index, value)
                {   


                  var nameValue=value.attributes[0].value;
                  var file = value.files[0];

                    if(file)
                    {
                         var formData = new FormData();

                        formData.append('file', file); 
                        //formData["name"] = nameValue;

                        $.ajax({
                          url : '/publicationservice-web/v1/publication/upload',
                          type : 'POST',
                          data : formData,
                          cache : false,
                          contentType : false,
                          processData : false,
                          name:nameValue,
                          success : function(data, textStatus, jqXHR) {
                                var message = jqXHR.responseText;
                                $("#messages").append("<li>" + message + "</li>");
                          },
                          error : function(jqXHR, textStatus, errorThrown) {
                                $("#messages").append("<li style='color: red;'>" + textStatus + "</li>");
                          }
                        });
                    }
                });
            });
        });
        </script>
</head>
<body>
    <h1>NGBulletin Upload System - Metadata and PDF</h1>

    <form action="v1/publication/upload" method="post" enctype="multipart/form-data">

        <p>
            Select XML file 1: <input type="file" name="metadata" id="metadata" size="45" accept=".xml" />
        </p>
        <p>
            Select PDF file 2: <input type="file" name="fileak" id="fileak" size="45"  accept=".pdf" />
        </p>

        <p>
            <input id="uploadBtn" type="button" value="Upload PFD Files" />
        </p>

    </form>

    <ul id="messages">   
    </ul>

</body>
</html>
Paul Samsotha

The thing is, the value in @FormDataParam("file") is the name. You use this when you want all the parts extracted by name. For instance you can have

post(@FormDataParam("metadata") InputStream metaIn,
     @FormDataParam("metadata") FormDataContentDisposition metaFcd,
     @FormDataParam("fileak") InputStream fileakIn,
     @FormDataParam("fileak") FormDataContentDisposition fileakFcd) {
}

But if you want to iterate through all the parts yourself, you should use FormDataMultiPart instead of List<FormDataBodyPart>. You can get the map, with the name as the key. Also the name is provided in the FormDataBodyPart.getName(). For example

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadAsset(FormDataMultiPart multipart) {

    Map<String, List<FormDataBodyPart>> map = multipart.getFields();

    for (Map.Entry<String, List<FormDataBodyPart>> entry : map.entrySet()) {

        for (FormDataBodyPart part : entry.getValue()) {
            InputStream in = part.getEntityAs(InputStream.class);
            String name = part.getName();
            System.out.println("--- name: " + name);
        }
    }
    return Response.ok("cool upload").build();
}

The point is that it is pretty pointless to try and obtain the name (programmatically) if you are going to use @FormDataParam annatotion, because you are ultimately already hard coding the name (in the annotation value), so you already know it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Jersey FormData, upload multiple file data

From Dev

Angular File upload formData

From Dev

Upload file without FormData

From Dev

React file upload with FormData

From Dev

File upload with ExtJS and Jersey

From Dev

Upload File with Jersey 2

From Dev

File upload with ExtJS and Jersey

From Dev

Cannot upload a file with jersey

From Dev

Unable to upload file to jersey

From Dev

Jersey restful web service - File upload with multiple object

From Dev

File Upload with jquery AJAX and FormData

From Dev

File upload with Jersey : FormDataContentDisposition is null

From Dev

MVC Multiple File Upload with Additional Data

From Dev

AJAX post multiple data after file upload

From Dev

Java Rest Jersey : Posting multiple types of data (File and JSON)

From Dev

Using Node Buffer or fileStream with formData file upload

From Dev

PHP file upload, sending formdata object

From Java

How to use FormData for AJAX file upload?

From Dev

AngularJS file upload sends stale formData

From Dev

how to upload a file in Restangularjs using multipart/formdata

From Dev

Jquery 1.10 ajax file upload formdata issue

From Dev

Upload file using FormData and jQuery.ajax

From Dev

How to configure Swagger UI, Jersey and file upload?

From Dev

How to get only the filename with Jersey File Upload

From Dev

Jersey File Upload Service missing dependency error

From Dev

File Upload Jersey Multipart Not Working in MULE 3.5.0

From Dev

org.glassfish.jersey upload file with FormDataContentDisposition

From Dev

File Wrong Upload Jersey RestFull Service

From Dev

Jersey File Upload Service missing dependency error