简体   繁体   中英

Sending Large Image in chunks

我正在将图像从我的android客户端发送到java jersey restful service,但我成功完成了。但是我的问题是,当我尝试发送大于1MB的大图像时,它消耗了更多的时间,所以我想在CHUNKS中发送图像, 有人可以帮我在执行此操作时。如何将CHUNKS中的( POST )图像流发送到服务器

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 ! :)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM