Download pdf file from JAX-RS

In JAX-RS, for pdf file, annotate the method with @Produces("application/pdf") :

  • Put @Produces(“application/pdf”) on service method.
  • Set “Content-Disposition” in Response header to prompt a download box.

1. Download Pdf file in JAX-RS

Full example to download a pdf file from JAX-RS.

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. Demo

Access this URI pattern : “/pdf/get“.

Figure : Pdf file “c:\\Android-Book.pdf” from server is prompted for user to download, with a new file name “new-android-book.pdf“
download pdf file from server

吸引蜘蛛小工具 1.00 The file is downloaded from Huajun Download Site 本软件下载自华军软件园 ╭═══════════════╮ ║ 华军软件园 ║ ╭══════┤ http://www.newhua.com ├══════╮ ║ ║ http://www.onlinedown.com ║ ║ ║ ╰═══════════════╯ ║ ║ ╭───────────────────────╮ ║ ╰══┤ 华军软件商城:http://www.pcsoft.com.cn ├══╯ ╰───────────────────────╯ ================================================================   华军软件园(www.newhua.com)是中国起步较早,口碑良好的大型 专业软件网站。是中国最具影响力的著名网站之一,目前已在全国一半以 上大中城市设立镜像站点及独立下载服务器且还在不断增加中以保证全国 各地区用户浏览、下载的速度,是国内更新速度最快、软件数量最多、软 件版本最新的共享免费软件下载和发布中心。 Huajun software garden is a big professional software website which startedearly and gets a good reputation, and is one of the most effective and famous website in China.It has now established its mirror image and independent downloading server in more than half of the big cities all over the country, and the number is still increasing in order to guarantee the speed of browsing and downloading. It is the fastest in the renovating speed, and owns the most number of softwares, also the newest sharing, downloading and publishing center for free software editions.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值