Sending Large Image in chunks

让人想犯罪 __ 提交于 2019-12-13 06:43:06

问题


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 more time so I like to send image in CHUNKS can anyone help me in doing this.How to send(POST) image stream in CHUNKS to server


回答1:


references used :

  • server code & client call
  • server function name

    /*** SERVER SIDE CODE****/
    
     @POST
     @Path("/upload/{attachmentName}")
     @Consumes(MediaType.APPLICATION_OCTET_STREAM)
        public void uploadAttachment(
                @PathParam("attachmentName") String attachmentName, 
                @FormParam("input") InputStream attachmentInputStream) {               
        InputStream content = request.getInputStream();
    
    // do something better than this
    OutputStream out = new FileOutputStream("content.txt");
    byte[] buffer = new byte[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);
      WebResource rootResource = client.resource("your-server-base-url");
        File file = new File("your-file-path");
       InputStream fileInStream = new FileInputStream(file);
        String contentDisposition = "attachment; filename=\"" + file.getName() + "\"";
        ClientResponse response = 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 ! :)




回答2:


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



来源:https://stackoverflow.com/questions/24054200/sending-large-image-in-chunks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!