Jersey client to download and save file

前端 未结 4 1673
一生所求
一生所求 2021-01-18 00:00

I Am new to jersey/JAX-RS implementation. Please find below my jersey client code to download file:

 Client client = Client.create();
 WebResource wr = clien         


        
4条回答
  •  再見小時候
    2021-01-18 00:46

    Here's another way of doing it using Files.copy().

        private long downloadReport(String url){
    
                long bytesCopied = 0;
                Path out = Paths.get(this.fileInfo.getLocalPath());
    
                try {
    
                     WebTarget webTarget = restClient.getClient().target(url);
                     Invocation.Builder invocationBuilder = webTarget.request(MediaType.TEXT_PLAIN_TYPE);
    
                     Response response = invocationBuilder.get();
    
                     if (response.getStatus() != 200) {
                        System.out.println("HTTP status " response.getStatus());
                        return bytesCopied;
                     }
    
                     InputStream in = response.readEntity( InputStream.class );
                     bytesCopied = Files.copy(in, out, REPLACE_EXISTING);
    
                     in.close();
    
                } catch( IOException e ){
                     System.out.println(e.getMessage());
                }
    
                return bytesCopied;
         }
    

提交回复
热议问题