有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java以块的形式发送大图像

我将图像从安卓客户端发送到java jersey restful服务,我成功地做到了这一点。但我的问题是当我试图发送大图像时,比如>;1MB占用更多时间,所以我喜欢以块发送图像有人能帮我吗。如何将(POST)图像流分块发送到服务器


共 (2) 个答案

  1. # 1 楼答案

    另一个路径是可用的,如果您不想过多地考虑使用: file upload apache太好了!:)

  2. # 2 楼答案

    使用的参考资料:

    • 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

    享受!:)