Jersey client download and save file

I am new to Jersey / JAX -RS implementation. The following is the client code for downloading the file:

Client client = Client.create(); WebResource wr = client.resource("http://localhost:7070/upload-0.0.1-SNAPSHOT/rest/files/download"); Builder wb=wr.accept("application/json,application/pdf,text/plain,image/jpeg,application/xml,application/vnd.ms-excel"); ClientResponse clientResponse= wr.get(ClientResponse.class); System.out.println(clientResponse.getStatus()); File res= clientResponse.getEntity(File.class); File downloadfile = new File("C://Data/test/downloaded/testnew.pdf"); res.renameTo(downloadfile); FileWriter fr = new FileWriter(res); fr.flush(); 

My server code:

 @Path("/download") @GET @Produces({"application/pdf","text/plain","image/jpeg","application/xml","application/vnd.ms-excel"}) public Response getFile() { File download = new File("C://Data/Test/downloaded/empty.pdf"); ResponseBuilder response = Response.ok((Object)download); response.header("Content-Disposition", "attachment; filename=empty.pdf"); return response.build(); } 

In my client code, I get the answer as 200 OK, but I can’t save the file to my hard drive. In the line below, I mention the path and place where the files should be saved. Not sure what is going wrong here, any help would be appreciated. Thanks in advance.

 File downloadfile = new File("C://Data/test/downloaded/testnew.pdf"); 
+6
source share
4 answers

I don't know if Jersey could just answer with a file like you are here:

 File download = new File("C://Data/Test/downloaded/empty.pdf"); ResponseBuilder response = Response.ok((Object)download); 

You can use StreamingOutput response to send a file from the server, for example:

 StreamingOutput stream = new StreamingOutput() { @Override public void write(OutputStream os) throws IOException, WebApplicationException { Writer writer = new BufferedWriter(new OutputStreamWriter(os)); //@TODO read the file here and write to the writer writer.flush(); } }; return Response.ok(stream).build(); 

and your client will wait for the stream to read and put it in a file:

 InputStream in = response.getEntityInputStream(); if (in != null) { File f = new File("C://Data/test/downloaded/testnew.pdf"); //@TODO copy the in stream to the file f System.out.println("Result size:" + f.length() + " written to " + f.getPath()); } 
+4
source

For people who are still looking for a solution, here is the complete code on how to save the jaxrs response to a file.

 public void downloadClient(){ Client client = ClientBuilder.newClient(); WebTarget target = client.target("http://localhost:7070/upload-0.0.1-SNAPSHOT/rest/files/download"); Response resp = target .request("application/pdf,image/jpeg,application/xml,application/vnd.ms-excel") .get(); if(resp.getStatus() == Response.Status.OK.getStatusCode()) { InputStream is = resp.readEntity(InputStream.class); fetchFeed(is); //fetchFeedAnotherWay(is) //use for Java 7 IOUtils.closeQuietly(is); System.out.println("the file details after call:"+downloadfile.getAbsolutePath()+", size is "+downloadfile.length()); } else{ throw new WebApplicationException("Http Call failed. response code is"+resp.getStatus()+". Error reported is"+resp.getStatusInfo()); } } /** * Store contents of file from response to local disk using java 7 * java.nio.file.Files */ private void fetchFeed(InputStream is){ File downloadfile = new File("C://Data/test/downloaded/testnew.pdf"); byte[] byteArray = IOUtils.toByteArray(is); FileOutputStream fos = new FileOutputStream(downloadfile); fos.write(byteArray); fos.flush(); fos.close(); } /** * Alternate way to Store contents of file from response to local disk using * java 7, java.nio.file.Files */ private void fetchFeedAnotherWay(InputStream is){ File downloadfile = new File("C://Data/test/downloaded/testnew.pdf"); Files.copy(is, downloadfile.toPath(), StandardCopyOption.REPLACE_EXISTING); } 
+2
source

This sample code can help you.

fooobar.com/questions/57621 / ...

This is a JAX RS breakdown service and test client. It reads the bytes from the file and loads the bytes into the REST service. The REST service encrypts bytes and sends them as bytes to the client. The client reads the bytes and saves the archived file. I posted this as a response to another thread.

+2
source

Here's another way to do this with 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; } 
0
source

Source: https://habr.com/ru/post/972123/


All Articles