我正在将图像从我的 android 客户端发送到 java jersey restful 服务,我成功地做到了这一点。但我的问题是当我尝试发送大图像时说 > 1MB 它消耗更多时间所以我喜欢发送图像 CHUNKS 谁能帮我做这件事。如何将 CHUNKS 中的图像流发送(POST)到服务器
最佳答案
引用文献:
- server code & client call
-
/*** 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);
你应该在客户端拆分文件,在服务器端恢复部分文件。 然后你应该将文件合并在一起。看看split /merge file on coderanch
尽情享受吧! :)
关于java - 分块发送大图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24054200/