Java 使用休息服务在浏览器中显示 pdf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32200862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:08:56  来源:igfitidea点击:

Display pdf in browser using a rest service

javarestpdfjersey

提问by jenny

I am generating a pdf with japser reports and I would like to create a REST web service that will return this pdf and display it in the browser. I have already tried the code displayed here:

我正在用 japser 报告生成一个 pdf,我想创建一个 REST web 服务,它将返回这个 pdf 并将其显示在浏览器中。我已经尝试过这里显示的代码:

REST web services method to display pdf file in browser

在浏览器中显示 pdf 文件的 REST Web 服务方法

But in this way the pdf file is downloaded. I would prefer it to be displayed in the browser first and then if the user wants he could download it later.

但是通过这种方式下载了pdf文件。我希望它首先显示在浏览器中,然后如果用户想要他可以稍后下载它。

(sorry for the duplicate question, but as you can see the above question has not been answered... )

(对不起,重复的问题,但正如您所见,上述问题尚未得到解答......)

EDIT:

编辑:

Working REST Service Code:

工作 REST 服务代码:

@GET
@Path("/pdf")
@Produces("application/pdf")
public javax.ws.rs.core.Response getPdf() throws Exception
{
    File file = new File("E:\tmp\test.pdf");
    FileInputStream fileInputStream = new FileInputStream(file);
    javax.ws.rs.core.Response.ResponseBuilder responseBuilder = javax.ws.rs.core.Response.ok((Object) fileInputStream);
    responseBuilder.type("application/pdf");
    responseBuilder.header("Content-Disposition", "filename=test.pdf");
    return responseBuilder.build();
}

采纳答案by Saurabh Jhunjhunwala

change

改变

response.header("Content-Disposition",  "attachment; filename=restfile.pdf");

to

response.header("Content-Disposition",  "filename=restfile.pdf");

回答by Branislav Lazic

You can easy do that with JasperReportsViewResolver. Presuming that you have data source defined, create a bean definition like this:

您可以使用JasperReportsViewResolver轻松做到这一点。假设你已经定义了数据源,创建一个像这样的 bean 定义:

@Autowired
private DataSource dataSource;

@Bean
public JasperReportsViewResolver getJasperReportsViewResolver() {
    JasperReportsViewResolver resolver = new JasperReportsViewResolver();
    resolver.setPrefix("classpath:/reports/");
    resolver.setSuffix(".jrxml");
    resolver.setJdbcDataSource(dataSource);
    resolver.setViewNames("rpt_*");
    resolver.setViewClass(JasperReportsMultiFormatView.class);
    resolver.setOrder(0);
    return resolver;
}

Your reports will be in i.e. /reports/directory and your reports will start with prefix rpt_. So in reportsfolder, you will put your JRXMLfiles.

您的报告将在 ie/reports/目录中,您的报告将以 prefix 开头rpt_。所以在reports文件夹中,您将放置您的JRXML文件。

And your request mapping method will look like this i.e.:

您的请求映射方法将如下所示:

@RequestMapping(value = "/report", method = RequestMethod.GET)
    public ModelAndView showReport() {
        ModelMap modelMap = new ModelMap();
        modelMap.put("format", "pdf");
        ModelAndView modelAndView = new ModelAndView("rpt_myReport", modelMap);
        return modelAndView;
}

You can basically threat your report like just another view. File will be opened in new tab with ability to download it, print it etc.

您基本上可以像另一种视图一样威胁您的报告。文件将在新选项卡中打开,可以下载、打印等。

回答by JoseAlv

Following the example of @Branislav Lazic, the way i found is to @Autowiredthe JasperReportsViewResolverin your report controller:

继@Branislav Lazic的例子中,我找到了办法是@AutowiredJasperReportsViewResolver报表中的控制器:

@Autowired
private JasperReportsViewResolver jrvr;

And override (or update) the headder propertie "Content-Disposition". For example:

并覆盖(或更新) header 属性"Content-Disposition"。例如:

...
final Properties properties = new Properties();
properties.setProperty("Content-Disposition", "attachment; filename=newReportName.xlsx");
jrvr.setHeaders(properties);
...

I don′t know if its the best way but works fine for me.

我不知道这是否是最好的方法,但对我来说效果很好。

回答by chandrashekar.n

Using InputStream first we can load the file into inputstream, then pass to IOUtils.copy and use this response.header("Content-Disposition", "attachment; filename=restfile.pdf")for download, use this for previewresponse.setHeader("Content-disposition", " filename=" + output)

首先使用 InputStream 我们可以将文件加载到 inputstream 中,然后传递给 IOUtils.copy 并使用它进行下载,使用它进行预览response.header("Content-Disposition", "attachment; filename=restfile.pdf")response.setHeader("Content-disposition", " filename=" + output)

    InputStream inputStream = new FileInputStream(new File(path)); // load the file
    IOUtils.copy(inputStream, response.getOutputStream());

    response.setContentType("application/pdf");
    response.setHeader("Content-disposition", " filename=" + output);

    response.flushBuffer();

Return type of the method is `ResponseEntity

方法的返回类型是`ResponseEntity