Jitendra Kumawat Jitendra Kumawat - 1 year ago 175
Java Question

Jersey Image Upload Client

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

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);
WebResource wr = client

String contentDisposition = "attachment; filename=\""
+ fileDetail.getName() + "\"";
FormDataMultiPart form = new FormDataMultiPart();
ContentDisposition contentDisposition2 = new ContentDisposition(contentDisposition);
FormDataBodyPart fdp = new FormDataBodyPart("file",
uploadedInputStream, MediaType.MULTIPART_FORM_DATA_TYPE);
ClientResponse response = wr.type(MediaType.MULTIPART_FORM_DATA).post(
ClientResponse.class, form)

Please help me not sure what i am missing here.

Answer Source

this full example to upload image using jersey client and webservice you client code

public class Test {

    private static URI getBaseURI() {
        return UriBuilder.fromUri("http://localhost:8080/restfullwebservice/resources/generic").build("");

    public static void main(String[] args) throws FileNotFoundException {
        final ClientConfig config = new DefaultClientConfig();
        final Client client = Client.create(config);

        final WebResource resource = client.resource(getBaseURI()).path("upload");

        final File fileToUpload = new File("C:/Users/Public/Pictures/Desert.jpg");

        final FormDataMultiPart multiPart = new FormDataMultiPart();
        if (fileToUpload != null) {
            multiPart.bodyPart(new FileDataBodyPart("file", fileToUpload,

        final ClientResponse clientResp = resource.type(
        System.out.println("Response: " + clientResp.getClientResponseStatus());


your webservice

public void uploadFile(@FormDataParam("file") InputStream uploadedInputStream, @FormDataParam("file") FormDataContentDisposition fileDetail) throws ServiceChannelException {
    OutputStream os = null;
    try {
        File fileToUpload = new File("C:/Users/Public/Pictures/Desert1.jpg");
        os = new FileOutputStream(fileToUpload);
        byte[] b = new byte[2048];
        int length;
        while ((length = uploadedInputStream.read(b)) != -1) {
            os.write(b, 0, length);
    } catch (IOException ex) {
        Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
        } catch (IOException ex) {
            Logger.getLogger(GenericResource.class.getName()).log(Level.SEVERE, null, ex);

Full application https://github.com/abdotalaat/upladeimageusingjersy

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download