I am trying to upload image using Jersey webservice , i am using jersey client to upload image. below is jersey web service which takes inputstream and uploads image on server. it works fine when i directly call it using jsp multipart form upload but fails when i upload images using jersey client
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) throws ServiceException
{
// upload code
}
Below is Jersey Client to upload Image, the client code is part of another web service which is called from php rest client and this jersey client call to jersey web service to upload image, if i directly call jersey web service to upload image that work's fine but it is not working when i upload using jersey client.
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
client.setChunkedEncodingSize(1024);
WebResource wr = client
.resource("http://localhost:8080/rest/upload");
String contentDisposition = "attachment; filename=\""
+ fileDetail.getName() + "\"";
FormDataMultiPart form = new FormDataMultiPart();
ContentDisposition contentDisposition2 = new ContentDisposition(contentDisposition);
form.setContentDisposition(contentDisposition2);
FormDataBodyPart fdp = new FormDataBodyPart("file",
uploadedInputStream, MediaType.MULTIPART_FORM_DATA_TYPE);
form.bodyPart(fdp);
ClientResponse response = wr.type(MediaType.MULTIPART_FORM_DATA).post(
ClientResponse.class, form)
Please help me not sure what i am missing here. Thanks.
FileInputStream
type foruploadedInputStream
and it works fine. One thing I would change (though it didn't cause it to fail for me) isMediaType.MULTIPART_FORM_DATA_TYPE
forfdp
toMediaType.APPLICATION_OCTET_STREAM_TYPE
– Mane