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 !!
You can’t just give a
Fileas the entity, it doesn’t work like that.You need to read the file yourself and give the data (as a
byte[]) as the entity.Edit:
You might also want to look at streaming the output. This has two advantages; 1) it allows you to use serve files without the memory overhead of having to read the whole file and 2) it starts sending data to the client straight away without you having to read the whole file first. See https://stackoverflow.com/a/3503704/443515 for an example of streaming.