Jersey client to download and save file

一笑奈何 提交于 2019-12-01 16:58:00
Paul Jowett

I don't know if Jersey let's you simply respond with a file like you have here:

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

You can certainly use a StreamingOutput response to send the file from the server, like this:

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 would expect to read a stream 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());
}

For folks still looking for a solution, here is the complete code on how to save 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);
}
RuntimeException

This sample code below may help you.

https://stackoverflow.com/a/32253028/15789

This is a JAX RS rest service, and test client. It reads bytes from a file and uploads the bytes to the REST service. The REST service zips the bytes and sends it back as bytes to the client. The client reads the bytes and saves the zipped file. I had posted this as a response to another thread.

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;
     }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!