I need to download pdf file using Jersey Web Services i already do the following but the file size received is always 0 (zero).
@Produces({"application/pdf"})
@GET
@Path("/pdfsample")
public Response getPDF() {
File f = new File("D:/Reports/Output/Testing.pdf");
return Response.ok(f, "application/pdf").build();
}
Please help to do the correct way, thanks !!
Jersey file download demoIf you hit the URL, “ http://localhost:8080/JerseyDemos/rest/download/pdf “, you will get below shown alert in your browser to download the file. The filename with which PDF file will be saved, will be what you set in Response.
Jersey contains a REST client library which can be used for testing or to build a real client in Java.
In JAX-RS, for user to download a file, annotate the method with @Produces("text/plain") : Put @Produces(“text/plain”) on service method, with a Response return type. It means the output is a text file. Set “Content-Disposition” in Response header to tell browser pop up a download box for user to download.
Mkyong always delivers. Looks like the only thing you are missing is the correct response header.
http://www.mkyong.com/webservices/jax-rs/download-excel-file-from-jax-rs/
@GET
@Path("/get")
@Produces("application/pdf")
public Response getFile() {
File file = new File(FILE_PATH);
ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition","attachment; filename=test.pdf");
return response.build();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With