I need to download pdf file using Jersey Web Services i already do the following but the file size received is always 0 (zero).
我需要使用Jersey Web Services下载pdf文件我已经执行以下操作,但收到的文件大小始终为0(零)。
@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 !!
请帮忙做正确的方法,谢谢!!
3 个解决方案
#1
4
You can't just give a File
as 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.
您需要自己读取文件并将数据(作为byte [])作为实体提供。
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://*.com/a/3503704/443515 for an example of streaming.
编辑:您可能还想查看流式输出。这有两个好处; 1)它允许你使用服务文件而不需要读取整个文件的内存开销; 2)它开始直接向客户端发送数据,而不必先读取整个文件。有关流媒体的示例,请参阅https://*.com/a/3503704/443515。
#2
8
Mkyong always delivers. Looks like the only thing you are missing is the correct response header.
Mkyong总是提供。看起来你唯一缺少的是正确的响应头。
http://www.mkyong.com/webservices/jax-rs/download-excel-file-from-jax-rs/
http://www.mkyong.com/webservices/jax-rs/download-excel-file-from-jax-rs/
#3
1
For future visitors,
对于未来的访客,
This will find the blob located at the passed ID and return it as a PDF document in the browser(assuming it's a pdf stored in the database):
这将找到位于传递的ID的blob并将其作为PDF文档返回到浏览器中(假设它是存储在数据库中的pdf):
@Path("Download/{id}")
@GET
@Produces("application/pdf")
public Response getPDF(@PathParam("id") Long id) throws Exception {
Entity entity = em.find(ClientCase.class, id);
return Response
.ok()
.type("application/pdf")
.entity(entity.getDocument())
.build();
}
#1
4
You can't just give a File
as 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.
您需要自己读取文件并将数据(作为byte [])作为实体提供。
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://*.com/a/3503704/443515 for an example of streaming.
编辑:您可能还想查看流式输出。这有两个好处; 1)它允许你使用服务文件而不需要读取整个文件的内存开销; 2)它开始直接向客户端发送数据,而不必先读取整个文件。有关流媒体的示例,请参阅https://*.com/a/3503704/443515。
#2
8
Mkyong always delivers. Looks like the only thing you are missing is the correct response header.
Mkyong总是提供。看起来你唯一缺少的是正确的响应头。
http://www.mkyong.com/webservices/jax-rs/download-excel-file-from-jax-rs/
http://www.mkyong.com/webservices/jax-rs/download-excel-file-from-jax-rs/
#3
1
For future visitors,
对于未来的访客,
This will find the blob located at the passed ID and return it as a PDF document in the browser(assuming it's a pdf stored in the database):
这将找到位于传递的ID的blob并将其作为PDF文档返回到浏览器中(假设它是存储在数据库中的pdf):
@Path("Download/{id}")
@GET
@Produces("application/pdf")
public Response getPDF(@PathParam("id") Long id) throws Exception {
Entity entity = em.find(ClientCase.class, id);
return Response
.ok()
.type("application/pdf")
.entity(entity.getDocument())
.build();
}