【问题标题】:Sending Large Image in chunks分块发送大图像
【发布时间】:2014-07-26 02:13:48
【问题描述】:

我正在将图像从我的 android 客户端发送到 java jersey restful 服务,并且我成功了。但我的问题是当我尝试发送大图像时说 > 1MB 它会消耗更多时间,所以我喜欢在 CHUNKS 谁能帮我做这件事。如何将 CHUNKS 中的图像流发送(POST)到服务器

【问题讨论】:

  • 回应了速度,因为这是一个非常充分的问题。给我反馈!享受

标签: java android web-services tomcat chunking


【解决方案1】:

使用的参考:

  • 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);
    

您应该在客户端拆分文件并在服务器中恢复部分文件。 之后,您应该将文件合并在一起。看看split /merge file on coderanch

享受吧! :)

【讨论】:

    【解决方案2】:

    如果您不想编写太多代码,请考虑使用其他路径: file upload apache 太好了! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多