Skip to content Skip to sidebar Skip to footer

Sending Large Image In Chunks

I am sending images from my android client to java jersey restful service and I succeded in doing that.But my issue is when I try to send large images say > 1MB its consumes mor

Solution 1:

references used :

  • server code & client call
  • server function name

    /*** SERVER SIDE CODE****/@POST@Path("/upload/{attachmentName}")@Consumes(MediaType.APPLICATION_OCTET_STREAM)publicvoiduploadAttachment(
                @PathParam("attachmentName") String attachmentName, 
                @FormParam("input") InputStream attachmentInputStream) {               
        InputStreamcontent= request.getInputStream();
    
    // do something better than thisOutputStreamout=newFileOutputStream("content.txt");
    byte[] buffer = newbyte[1024];
    int len;
    while ((len = in.read(buffer)) != -1) {
        // whatever processing you want here
        out.write(buffer, 0, len);
    }
     out.close();
    
     return Response.status(201).build();
    }
    
    
          /**********************************************//** 
       CLIENT SIDE CODE
     **/// .....
      client.setChunkedEncodingSize(1024);
      WebResourcerootResource= client.resource("your-server-base-url");
        Filefile=newFile("your-file-path");
       InputStreamfileInStream=newFileInputStream(file);
        StringcontentDisposition="attachment; filename=\"" + file.getName() + "\"";
        ClientResponseresponse= rootResource.path("attachment").path("upload").path("your-file-name")
              .type(MediaType.APPLICATION_OCTET_STREAM).header("Content-Disposition", contentDisposition)
             .post(ClientResponse.class, fileInStream);
    

You should split the file in the client and restore part of the file in the server. and after that you should merge the files together. Take a look at split /merge file on coderanch

Enjoy ! :)

Solution 2:

Another path is available, if you don't want to code too much consider using : file upload apache that is great ! :)

Post a Comment for "Sending Large Image In Chunks"