1

を使用してRESTアプリとPDFを作成してJasper Reportおり、ブラウザにPDFのファイルダウンロードダイアログを表示したいと考えています。

これはまさに私が探しているものです:
http://www.mkyong.com/webservices/jax-rs/download-pdf-file-from-jax-rs/

以下のコードは PDF ファイル (MyAwesomeJasperReport25.pdf) を作成しますが、ファイルのダウンロード ダイアログがブラウザーに表示されず、その理由がわかりません。

@GET
@Path("pdf")
@Produces("application/pdf")
public Response outputPDF() {

    OutputStream output = null;

    try {    
        File jrxmlFile = new File("C:\\Users\\m-takayashiki\\report2.jrxml");

        if(jrxmlFile.exists()) {

            //jrxml compile
            JasperReport jasperReport = JasperCompileManager.compileReport(jrxmlFile.getAbsolutePath());

            //some code emitted        

            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);

            String filePath = "C:\\Users\\m-takayashiki\\MyAwesomeJasperReport25.pdf";
            output = new FileOutputStream(new File(filePath)); 
            JasperExportManager.exportReportToPdfStream(jasperPrint, output); 


            // From here trying to ask user to download PDF        

            ResponseBuilder response = Response.ok((Object) filePath);

            response.header("Content-disposition",
                    "attachment; filename=MyAwesomeJasperReportDownload.pdf");

            return response.build();
        }
    }
    catch(Exception e) {
        System.out.println("-------------------- PDF exception ");
        System.out.println(e);
        return null;
    }
    finally {
        try {
            if(output != null) { output.close(); }
        }
        catch(Exception e) { System.out.println(e); }

    }
}
4

3 に答える 3

1

次の 2 点を確認します。

  1. 応答エンティティ ("Response.ok((Object) filePath)") に filePath の代わりに File のインスタンスを入れるべきではありませんか?
  2. JAX-RS 実装には File クラスのエンティティ プロバイダがありますか?
于 2012-05-09T18:12:49.167 に答える
-2

Ok()出力オブジェクトをメソッドに入れるのを忘れています:

ResponseBuilder response = Response.ok((Object) filePath);

正しい:

ResponseBuilder response = Response.ok((output) filePath);

于 2012-08-17T17:55:27.857 に答える