I have created a jersey restful web service where I managed to upload multiple number of files using @Context HttpServletRequest request
as method signature which work nicely.
Thing is, to fetch other form fields I need to repetitively check with .isFormField();
method with relative .getName();
for file or .getFieldName();
, and .getString();
method to check whether required fields are present or not every time the web service is called which I think little lengthy and expensive process if there are several other fields.
Easier approach was to use @FormDataParam
where webservice used to exposed with parameter which client need to pass but problem is I am not able to upload more than one file at one go.
Since Its also not possible to use request.getParameter("field1");
to get other form fields if media type or enctype is multipart/form-data
.
Whenever I tried to combine both @FormDataParam
and @Context HttpServletRequest request
together, it throws exception:
org.apache.tomcat.util.http.fileupload.FileUploadException: Stream closed
while parsing the request with .parseRequest(request);
method of ServletFileUpload
class.
Kindly suggest some good approach How can I achieve multiple file upload with getting required form fields as easy as @FormDataParam
in jersey.
approach for multiple file upload:
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@Path("/multipleFiles")
public String restDemo(@Context HttpServletRequest request)
{
//...code goes here
}
My form:
output:(after parsing request)
field1 > abc
field2 > xyz
Chrysanthemum.jpg Size: 879394
Desert.jpg Size: 845941
Hydrangeas.jpg Size: 595284
Jellyfish.jpg Size: 775702
@FormDataParam
? As per my knowledge is concerned mapping of multiple file in single@FormDataParam
is not possible. – Aman Jun 19 '13 at 13:38