Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8514539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:53:55+00:00 2026-06-11T04:53:55+00:00

I have successfully developed a service, in which I read files uploaded in a

  • 0

I have successfully developed a service, in which I read files uploaded in a multipart form in Jersey. Here’s an extremely simplified version of what I’ve been doing:

@POST
@Path("FileCollection")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail) throws IOException {
    //handle the file
}

This works just fine but I’ve been given a new requirement. In addition to the file I’m uploading, I have to handle an arbitrary number of resources. Let’s assume these are image files.

I figured I’d just provide the client with a form with one input for the file, one input for the first image and a button to allow adding more inputs to the form (using AJAX or simply plain JavaScript).

<form action="blahblahblah" method="post" enctype="multipart/form-data">
   <input type="file" name="file" />
   <input type="file" name="image" />
   <input type="button" value="add another image" />
   <input type="submit"  />
</form>

So the user can append the form with more inputs for images, like this:

<form action="blahblahblah" method="post" enctype="multipart/form-data">
   <input type="file" name="file" />
   <input type="file" name="image" />
   <input type="file" name="image" />
   <input type="file" name="image" />
   <input type="button" value="add another image" />
   <input type="submit"  />
</form>

I hoped it would be simple enough to read the fields with the same name as a collection. I’ve done it successfully with text inputs in MVC .NET and I thought it wouldn’t be harder in Jersey. It turns out I was wrong.

Having found no tutorials on the subject, I started experimenting.

In order to see how to do it, I dumbed the problem down to simple text inputs.

<form action="blahblabhblah" method="post" enctype="multipart/form-data">
   <fieldset>
       <legend>Multiple inputs with the same name</legend>
       <input type="text" name="test" />
       <input type="text" name="test" />
       <input type="text" name="test" />
       <input type="text" name="test" />
       <input type="submit" value="Upload It" />
   </fieldset>
</form>

Obviously, I needed to have some sort of collection as a parameter to my method. Here’s what I tried, grouped by collection type.

Array

At first, I checked whether Jersey was smart enough to handle a simple array:

@POST
@Path("FileCollection")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(@FormDataParam("test") String[] inputs) {
    //handle the request
}

but the array wasn’t injected as expected.

MultiValuedMap

Having failed miserably, I remembered that MultiValuedMap objects could be handled out of the box.

@POST
@Path("FileCollection")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(MultiValuedMap<String, String> formData) {
    //handle the request
}

but it doesn’t work either. This time, I got an exception

SEVERE: A message body reader for Java class javax.ws.rs.core.MultivaluedMap, 
and Java type javax.ws.rs.core.MultivaluedMap<java.lang.String, java.lang.String>, 
and MIME media type multipart/form-data; 
boundary=----WebKitFormBoundaryxgxeXiWk62fcLALU was not found.

I was told that this exception could be gotten rid of by including the mimepull library so I added the following dependency to my pom:

    <dependency>
        <groupId>org.jvnet</groupId>
        <artifactId>mimepull</artifactId>
        <version>1.3</version>
    </dependency>

Unfortunately the problem persists. It’s probably a matter of choosing the right body reader and using different parameters for the generic. I’m not sure how to do this. I want to consume both file and text inputs, as well as some others (mostly Long values and custom parameter classes).

FormDataMultipart

After some more research, I found the FormDataMultiPart class. I’ve successfully used it to extract the string values from my form

@POST
@Path("upload2")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadMultipart(FormDataMultiPart multiPart){
    List<FormDataBodyPart> fields = multiPart.getFields("test");
    System.out.println("Name\tValue");
    for(FormDataBodyPart field : fields){
        System.out.println(field.getName() + "\t" + field.getValue());
        //handle the values
    }
    //prepare the response
}

The problem is, this is a solution to the simplified version of my problem. While I know that every single parameter injected by Jersey is created by parsing a string at some point (no wonder, it’s HTTP after all) and I have some experience writing my own parameter classes, I don’t really how to convert these fields to InputStream or File instances for further processing.

Therefore, before diving into Jersey source code to see how these objects are created, I decided to ask here whether there is an easier way to read a set (of unknown size) of files. Do you know how to solve this conundrum?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:53:57+00:00Added an answer on June 11, 2026 at 4:53 am

    I have found the solution by following the example with FormDataMultipart. It turns out I was very close to the answer.

    The FormDataBodyPart class provides a method that allows its user to read the value as InputStream (or theoretically, any other class, for which a message body reader is present).

    Here’s the final solution:

    Form

    The form remains unchanged. I have a couple of fields with the same name, in which I can place files. It’s possible to use both multiple form inputs (you want these when uploading many files from a directory) and numerous inputs that share a name (Flexible way to upload an unspecified number of files from different location). It’s also possible to append the form with more inputs using JavaScript.

    <form action="/files" method="post" enctype="multipart/form-data">
       <fieldset>
           <legend>Multiple inputs with the same name</legend>
           <input type="file" name="test" multiple="multiple"/>
           <input type="file" name="test" />
           <input type="file" name="test" />
       </fieldset>
       <input type="submit" value="Upload It" />
    </form>
    

    Service – using FormDataMultipart

    Here’s a simplified method that reads a collection of files from a multipart form. All inputs with the same are assigned to a List and their values are converted to InputStream using the getValueAs method of FormDataBodyPart. Once you have these files as InputStream instances, it’s easy to do almost anything with them.

    @POST
    @Path("files")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadMultipart(FormDataMultiPart multiPart) throws IOException{        
        List<FormDataBodyPart> fields = multiPart.getFields("test");        
        for(FormDataBodyPart field : fields){
            handleInputStream(field.getValueAs(InputStream.class));
        }
        //prepare the response
    }
    
    private void handleInputStream(InputStream is){
        //read the stream any way you want
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have successfully developed a small iPhone+Monotouch (latest version) application with Monodevelop 2.8, which
I have successfully developed and deployed a ClickOnce application which registers an associated file
I have WCF service which launches the remote process from Process.Start successfully on stand
Im trying to create an ASMX Web Service in Visual Web Developer.I have successfully
I have developed a view based project in Xcode. It is successfully running in
I am using a Java Web Service which is developed by one of our
I am currently working on a Java EE project. I have successfully developed a
I have developed a sample WCF REST service that accepts that creates an Order
I have developed an app that has work successfully for the last 4 months
I have windows application developed based on WPF,i am going thur weird issue here.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.