在JAX-RS中,对于pdf文件,请使用@Produces("application/pdf")注释该方法:
- 将@Produces(“ application / pdf”)放在服务方法上。
- 在Response标头中设置“ Content-Disposition ”以提示下载框。
1.在JAX-RS中下载Pdf文件
从JAX-RS下载pdf文件的完整示例。
import java.io.File;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
@Path("/pdf")
public class PdfService {
private static final String FILE_PATH = "c:\\Android-Book.pdf";
@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=new-android-book.pdf");
return response.build();
}
}
2.演示
访问此URI模式:“ / pdf / get ”。
图片:服务器提示用户下载Pdf文件“ c:\\ Android-Book.pdf ”,新文件名为“ new-android-book.pdf ”
下载源代码
下载它– JAX-RS-Download-Pdf-File-Example.zip (6 KB)
参考文献
翻译自: https://mkyong.com/webservices/jax-rs/download-pdf-file-from-jax-rs/
本文介绍如何在JAX-RS中实现PDF文件的下载功能,通过使用@Produces注解和设置Content-Disposition响应头,使服务器能正确地提供PDF文件供用户下载。

625

被折叠的 条评论
为什么被折叠?



