问题
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