java - 分块发送大图像

标签 java android web-services tomcat chunking

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

最佳答案

引用文献:

  • 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

尽情享受吧! :)

关于java - 分块发送大图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24054200/

相关文章:

java - Gradle 2 android 项目使用相同的库

android - 如何在不以编程方式在 Android 中连接的情况下区分开放和安全 WiFi 网络?

java - IncationTargetException - 使用 httpClient 从 java Soap Web 服务调用 Rest 服务

java - 为什么我用java向文件写入数据这么慢

java - AWS S3 使用 putObject(String,String,String) 时更改文件扩展名

java - Spring Boot 和 ElasticSearch 文档中日期转换异常

java - 理论: Two system integration through web service

Android 在其他 xml 文件中触发按钮

android - Renderscript 模糊警告

java - Axis 故障 : (500)Internal Server Error