JAX-RSからpdfファイルをダウンロードする

JAX-RSでは、pdfファイルの場合、このメソッドに `@Produces(" application/pdf ")`という注釈を付けます。

  1. @ Produces( "application/pdf") をサービスメソッドに置きます.

  2. レスポンスヘッダに " 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();

    }

}

デモ

このURIパターンにアクセスします: " /pdf/get "。

__Figure:新しいファイル名「 new-android-book.pdf 」を使用して、サーバーからのPDFファイル「 c:\\ Android-Book.pdf

サーバーからpdfファイルをダウンロードする、title = "download-pdf-jax-rs"、width = 463、height = 353

ソースコードをダウンロードする

ダウンロードする - JAX-RS-Download-Pdf-File-Example.zip (6 KB)

参考文献

@Produces JavaDoc]。 http://en.wikipedia.org/wiki/Internet media type#Type__multipart[List

アプリケーションタイプの一覧]