2014-06-05 80 views
0

我從我的Android客戶端發送圖像到java澤西寧靜的服務,我成功地做到了這一點。但我的問題是,當我試圖發送大於1MB的大圖像,它消耗更多的時間,所以我喜歡在CHUNKS發送圖像誰能幫我做this.How在CHUNKS發送(POST)圖像流服務器使用發送大塊圖像

+0

響應速度,因爲是一個非常充足的問題。給我反饋!請享用 – jeorfevre

回答

0

引用:

  • 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

享受! :)