Jersey客户端下载并保存文件

cxy*_*xyz 8 java rest jax-rs jersey-2.0

我是jersey/JAX-RS实施的新手.请在下面找到我的球衣客户代码下载文件:

 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();
Run Code Online (Sandbox Code Playgroud)

我的服务器端代码是:

@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();
    }
Run Code Online (Sandbox Code Playgroud)

在我的客户端代码中我得到响应200 OK,但我无法将我的文件保存在硬盘上在下面的行中我提到了文件需要保存的路径和位置.不知道这里出了什么问题,任何帮助都会受到赞赏.谢谢!

File downloadfile = new File("C://Data/test/downloaded/testnew.pdf");
Run Code Online (Sandbox Code Playgroud)

Pau*_*ett 5

我不知道泽西岛是否让你回复一个像你这样的文件:

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

可以肯定使用StreamingOutput响应从服务器发送的文件,就像这样:

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();
Run Code Online (Sandbox Code Playgroud)

并且您的客户端希望读取流并将其放入文件中:

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());
}
Run Code Online (Sandbox Code Playgroud)


pNu*_*Nut 5

对于仍在寻找解决方案的人们,这里是有关如何保存jaxrs对文件的响应的完整代码。

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);
}
Run Code Online (Sandbox Code Playgroud)