Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Download PDF file using Jersey?

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 !!

like image 684
esthrim Avatar asked Dec 14 '12 10:12

esthrim


People also ask

How do I download a file from Jersey?

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.

What is Jersey library used for?

Jersey contains a REST client library which can be used for testing or to build a real client in Java.

Which of these will specify file download in Jaxrs?

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.


1 Answers

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();
}
like image 180
Joseph Helfert Avatar answered Oct 13 '22 08:10

Joseph Helfert