2014-03-06 119 views
4

我有一个Jersey服务,输出二进制数据为StreamingOutput,MediaType.APPLICATION_OCTET_STREAM泽西岛2客户端StreamingOutput

如何实现客户端使用Jersey 2来处理response这样的服务?

+0

有泽西文档客户端API部分,看看这个例如:https://jersey.java.net/documentation/latest/client.html#d0e4349 – vadchen

+0

谢谢。有用 – sunish

回答

0

下面是实现泽西2客户端从中返回二进制数据StreamingOutputMediaType.APPLICATION_OCTET_STREAM REST服务下载文件的一种方式 -

Client client = ClientBuilder.newClient(); 
    // change SERVER_URL, API_PATH and PATH as per REST API details 
    WebTarget webTarget = client.target(SERVER_URL).path(API_PATH).path(PATH); 

    Invocation.Builder invocationBuilder = webTarget.request(); 
    Response response = invocationBuilder.get(); 

    String contentDispositionHeader = response.getHeaderString("Content-Disposition"); 
    String fileName = contentDispositionHeader 
      .substring(contentDispositionHeader.indexOf("filename=") + "filename=".length()).replace("\"", ""); 

    InputStream responseStream = response.readEntity(InputStream.class); 

    // Set location here where you want to store downloaded file. 
    // It will replace the file if already exist in that location with same name. 
    Files.copy(responseStream, Paths.get("H:/"+ fileName), StandardCopyOption.REPLACE_EXISTING); 

    System.out.println("File is downloaded"); 
相关问题